Difference between Array & Vector
ANS1:- Array :
Array is most important thing in any programming language .Array is used to store the data items of the same kind .It means in array we can store the multiple values of same datatype belonging to a particular class. Array stores the values in memory at the fixed size. Multiple types of arrays are used in any programming language such as: one - dimensional, two - dimensional or can say multi - dimensional.
Declaration of an array :
int arr[] = new int [10];
sometimes , may be we can define the size or not .
Another way of array declaration is :
int arr[]={ 13,34,45,56};
A simple program of using array is given below :
import java.util.*;
public class ArrayClass{
public static void main(String[] args){
int arr[] = {14,45,56,10,67};
int l = arr.length;
int i,j;
System.out.print("numbers we have given are : ");
for (i=0;i<l;i++ ){
System.out.print(" " + arr[i]);
}
System.out.println("\n");
System.out.print("Accending order number : ");
Arrays.sort(arr);
for(i=0;i<l;i++){
System.out.print(" " + arr[i]);
}
}
}
output of the above program is
10
14
45
56
67
- - - - - - - - - - - - -
Vector:
For using vector we have to include java.util.Vector class. We create an object of Vector class and performs various operation like adding, removing etc. Vector class extends AbstractList and implements List, RandomAccess, Serializable interfaces . The size of a vector increase and decrease according to the program requirement .
Vector class contains seven multiplemethods such as .
add(Object o): Adds the element in end of the Vector
firstElement(): Returns the first element of the vector.
elementAt(int index): Returns the element at the specified index.
size(): Gives the number of element in a vector.
removeElementAt(int index): Deletes the element from the given index
elements(): Returns an enumeration of the element
lastElement(): Returns last element.
We can also used Enumeration interface to retrieve the value from vector.
Enumeration interface has two methods.
nextElement(): Checks the next element of the enumeration.
hasMoreElements(): Checks if this enumeration contains more elements or not.
- - - - - - - - - - - - -
Array Vs Vector :
Both Array and Vector in Java are similar. Both are used for storing the data. But there are some basisc difference between Array & Vector .
1. Array is the static memory allocation , while vector is the dynamic memory allocation.
2. Array allocates the memory for the fixed size ,in array there is wastage of memory, while Vector allocates the memory dynamically means according to the requirement no wastage of memory .
3. Vector has many more methods in comparing to Array.
4. By using Vector we can add, remove, & find the size of elements dynamically ,while it is not possible in Array.
Convert Array to Vector
http://www.roseindia.net/java/java-conversion/convert-vector-to-array.shtml