What is the Difference between "vector" and "array"?
Answers were Sorted based on User's Feedback
Answer / vishwa
vector is also an array but the size of a vector can change
dynamically where in array its fixed.
we can create some reserve space in vector where in array we
cannot.
Is This Answer Correct ? | 136 Yes | 17 No |
Answer / guest
a vector is also like an array i.e sequentially accessed but
we can increase/decrease size of a vector in a simple way
than it is done for arrays.
Is This Answer Correct ? | 79 Yes | 39 No |
Answer / saroj das ,
as we know in C++ the size of array is fixed in compile
time .so the size of array cannot be adjusted at run time
to accommodate changeling program .
but vector can solve this problem allocating memory as
needed. although vector is dynamic
Is This Answer Correct ? | 48 Yes | 10 No |
Answer / sanish joseph
A growable array is calld vector while array is of fixed size,
Is This Answer Correct ? | 41 Yes | 12 No |
Answer / raju
We know that arrays and Vectors in Java are different kinds
of data structures for storing information in a
random-access fashion. In Java, an object-oriented language,
the two structures are implimented as classes. This
presentation will demonstrate some of the principle
differences between Java arrays and Java Vectors. It will
also provide some examples of the cool methods that come
with them.
Arrays
Arrays in Java are static lists declared to store a certain
number of a certain kind of variables. It stores these
values at specific, numbered locations in a list that starts
at zero and goes to N-1:
myArray Index Data
0 14
1 79
2 32
3 92
4 102
This array can be instantiated in Java with either the
shorthand syntax:
int[] myArray = new int[5];
int myArray[] = new int[5];
or an explicit method call to a Java "Array" class object:
int[] myArray = Array.newInstance[int, 5];
When an array is instantiated, the array's constructor
method allocates 5 integer-sized memory slots. The
allocation is static- the array size cannot change after it
is created. In this sense, it is analagous to a C or C++
style array. But the resemblence doesn't end here. Java
Arrays, like C arrays, have undefined contents until they
are initialized. One can set the contents of an array at the
time of instantiation with the simple syntax:
int[] myArray = {14,34,21,42,22};
We have already seen that, in addition to the built-in
C-style array operations syntax there Java provides
additional classes for manipulating arrays. This represents
a desire on the Java authors' part to both a) retain a
grounding in C to attract those programmers already familiar
with the popular language, and b) provide true
object-oriented support for array operations. This is true
for array referencing, as well. Various array-handling
objects exist in the class library java.lang. This provides
interface functions for various array operations For
example, one could refer to the individual array as in C:
fourth_item = myArray[3];
or, one could use the explicit method call:
fourth_item = myArray[3];
Java arrays can also be passed as parameters to functions.
Java permits either a copy of the original array or a
pointer to be sent.
2D-arrays are also supported. This is a statically-allocated
array of array pointers:
int[][] 2D_array = new int[x_length][y_length];
Finally, note that vectors of objects can exist. This does
not mean, however, that the new objects are instantiated
when the array is declared. Arrays take up a fixed size in
memory, and the size of an object isn't defined when it is
instantiated. Therefore, the objects must be instantiated in
a seperate command. Therefore, to create an array of 5
objects, the following code could be used:
object_list = new Object[5];
object_list[0] = new Object();
object_list[1] = new Object();
.
.
.
Vectors
The key difference between Arrays and Vectors in Java is
that Vectors are dynamically-allocated. They aren't declared
to contain a type of variable; instead, each Vector contains
a dynamic list of references to other objects. The Vector
class is found in the java.util package, and extends
java.util.Abstractlist.
The big advantage of using Vectors is that the size of the
vector can change as needed. Vectors handle these changes
through the "capacity" and "capacityIncrement" fields. When
a Vector is instantiated, it declares an object array of
size initialCapacity. Whenever this array fills, the vector
increases the total buffer size by the value
capacityIncrement. Thus, a Vector represents a compromise.
It doesn't allocate each object dynamically, which would
make access of middle list-items exceedingly slow; but it
does allow for growth in the size of the list. The default
constructor, Vector(), creates an empty Vector of initial
capacity zero that doubles in size whenever it fills.
Vectors can be filled using its method addElement(Object
obj). This method appends the given object to the end of the
Vector. Because the Vector stores pointers to the objects,
and not the objects themselves, these Vector items could be
any kind of object. Moreover, several different kinds of
objects could exist in the same Vector. Vector methods allow
all sorts of nifty ways to manipulate data. For example, one
can reference a list item not just by its index, but also by
name. Elements can be removed from or added to the middle of
the list:
myVector.insertElementAt(my_object, 5);
success_flag = myVector.remove(my_object);
So you're thinking, "GREAT! But if vectors are so wonderful,
why even bother with arrays?"
Comparative Advantages of Arrays and Vectors
Because Vectors are dynamically-allocated, they offer a
relatively memory-efficient way of handling lists whose size
could change drastically. The line buffer of a text editor,
for example, could be anywhere from zero to thousands of
entries in size. A Vector might be a good choice for such an
arrangement. Moreover, Vectors have some useful member
functions that make coding simpler (see the insertElementAt
and remove methods above). But because the Vector class is
based on an array of object references, these methods are
generally no more efficient than array-based algorithms. The
insert method must perform multiple "swap" operations just
as an array 'insert' algorithm would. Thus, Vectors are
easier to use than arrays for most applications, but they do
not offer all the performance advantages of fully-dynamic
storage.
Is This Answer Correct ? | 27 Yes | 9 No |
Answer / kabitirtha koley
The basic difference between those two are:
1.Vector and Arraylist are grownable or shinkable where are
array is not.
2.Vector and Arraylist are implemented from List interface
where as array is a primitive data type
3.Vector is Synchrnized where as arraylist is not
4.For best performance better to use arraylist than vector
Is This Answer Correct ? | 14 Yes | 4 No |
Answer / mms zubeir
First, a vector is a template class but not the array is.
So obviously there are members to access the elements of
the vector like, range checking, iterators,
insertion/deletion, predicates, etc.
Since it is a template class, the same implementation can
be used for any type including pointers, objects/user
defined types.
Second, as said by others, vector automatically grow as we
invoke push_back(), we can reserve additional space for
elements, etc.
Is This Answer Correct ? | 18 Yes | 12 No |
Answer / avinash moras
arrays have definite size and cannot be increased once
declared....whereas the size of vectors can be increased
once it is on the mark to get filled...vectors ae declared
in java.util package*;
Is This Answer Correct ? | 10 Yes | 4 No |
Answer / namitha
Vectors are a kind of sequence containers. As such, their
elements are ordered following a strict linear sequence.
Vector containers are implemented as dynamic arrays; Just as
regular arrays, vector containers have their elements stored
in contiguous storage locations, which means that their
elements can be accessed not only using iterators but also
using offsets on regular pointers to elements.
Arrays is a sequence of similar data type.
Is This Answer Correct ? | 11 Yes | 6 No |
Answer / katty
In 'vector' memory allocated(dynamically) during runtime and
for array memory allocated during compilation.
Is This Answer Correct ? | 5 Yes | 1 No |
What is an operator function? Describe the function of an operator function?
What is the use of ‘using’ declaration?
Can constructor be private in c++?
Write a C++ program which will compute the volume of a sphere or a cylinder after prompting the user to type the first character for the shape name.
0 Answers An-Najah National University,
If you hear the cpu fan is running and the monitor power is still on, but you did not see anything show up in the monitor screen. What would you do to find out what is going wrong?
Brief explaination about #include<iostream.h>, cin and cout
What is the best it certification?
What happens if an exception is throws from an object's constructor and from object's destructor?
What are friend classes? What are advantages of using friend classes?
Explain container class.
What is constructor in C++?
What is different in C++, compare with unix?