What is the Difference between "vector" and "array"?

Answers were Sorted based on User's Feedback



What is the Difference between "vector" and "array"?..

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

What is the Difference between "vector" and "array"?..

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

What is the Difference between "vector" and "array"?..

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

What is the Difference between "vector" and "array"?..

Answer / sanish joseph

A growable array is calld vector while array is of fixed size,

Is This Answer Correct ?    41 Yes 12 No

What is the Difference between "vector" and "array"?..

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

What is the Difference between "vector" and "array"?..

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

What is the Difference between "vector" and "array"?..

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

What is the Difference between "vector" and "array"?..

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

What is the Difference between "vector" and "array"?..

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

What is the Difference between "vector" and "array"?..

Answer / katty

In 'vector' memory allocated(dynamically) during runtime and
for array memory allocated during compilation.

Is This Answer Correct ?    5 Yes 1 No

Post New Answer

More C++ General Interview Questions

Is c++ a float?

0 Answers  


How to change constant values?

6 Answers   Huawei, Symphony,


What are separators in c++?

0 Answers  


What is the use of endl in c++?

0 Answers  


What is the difference between global int and static int declaration?

0 Answers  






Can we get the value of ios format flags?

0 Answers  


How const int *ourpointer differs from int const *ourpointer?

0 Answers  


Define a constructor - what it is and how it might be called (2 methods)?

0 Answers  


How can you force instantiation of a template?

1 Answers   ABC, Amazon,


Explain queue. How it can be implemented?

0 Answers  


what is an array

17 Answers  


What is the difference between the parameter to a template and the parameter to a function?

0 Answers  


Categories