:: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Categories >> Software >> Programming Languages >> C++ >> C++ General


 STL interview questions  STL Interview Questions (93)
 OOPS interview questions  OOPS Interview Questions (562)
 C++ General interview questions  C++ General Interview Questions (459)
What is the Difference between "vector" and "array"?
 Question Submitted By :: C++-General
I also faced this Question!!     Answer Posted By  
# 1
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
Is This Answer Correct ?    132 Yes 17 No
# 2
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 ?    78 Yes 39 No
# 3
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 ?    47 Yes 10 No
Saroj Das ,
# 4
A growable array is calld vector while array is of fixed size, 
Is This Answer Correct ?    41 Yes 12 No
Sanish Joseph
# 5
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 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();

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

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
Is This Answer Correct ?    27 Yes 8 No
# 6
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
Kabitirtha Koley
# 7
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
Mms Zubeir
# 8
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 5 No
# 9
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
Avinash Moras
# 10
In 'vector' memory allocated(dynamically) during runtime and
for array memory allocated during compilation.
Is This Answer Correct ?    4 Yes 1 No

Other C++ General Interview Questions
  Question Asked @ Answers
Explain about profiling?   1
what is meaning of isa and hsa   1
How size of a class can be calulated?   2
Explain the difference between 'operator new' and the 'new' operator? Lucent 1
founder of c++ Microtek 7
Why did you leave your last job?   2
What C++ libraries are you proficient with? Google 1
structure contains int, char, float how it behaves for big endian and little endian? BITS 1
if there is binary tree which one is the easiest way to delete all child node? Persistent 1
How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Lucent 5
What is Namespace? Samsung 4
Can we declare destructor as static? Explain?   3
For more C++ General Interview Questions Click Here 

Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
Copyright 2013  All Rights Reserved.   ::