Difference between vector and array in Java
In this post, you will learn about the difference between vector and array using Java programming language.
Vector in Java
The vector class is used in Java to store data using the List interface. It can grow or shrink in size. It is an implementation of the List interface that allows us to create resizable-arrays. Vector implements a dynamic array, which means it can grow or shrink as required. The vector is synchronized, which means only one thread at a time can access the code. The vector class is generally used in the thread-safe implementation. It provides some useful methods like add(), addAll(), capacity(), clear(), clone(), elements(), equals(), size(), set().
Vector class Declaration
public class Vector<E>
extends Object<E>
implements List<E>, Cloneable, Serializable
Array in Java
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. In Java, all arrays are dynamically allocated. A Java array variable can also be declared like other variables with [] after the data type.
Example of Vector in Java
import java.util.*;
public class JavaVector{
public static void main(String args[]){
Vector vec=new Vector();//creating vector
vec.add("Smith");
vec.addElement("Pepa");
vec.addElement("Jorz");
vec.addElement("Andy");
//traversing elements using Enumeration
Enumeration enm=vec.elements();
while(enm.hasMoreElements()){
System.out.println(enm.nextElement());
}
}
}
Output of the above code:
Smith
Pepa
Jorz
Andy
Example of Array in Java
public class TestJavaArray
{
public static void main (String[] args)
{
// declares an array of integers.
int[] arr;
// allocating memory for 7 integers.
arr = new int[7];
// initialize the first element
arr[0] = 89;
// initialize the second element
arr[1] = 12;
// and so on...
arr[2] = 79;
arr[3] = 20;
arr[4] = 83;
arr[5] = 42;
arr[6] = 39;
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i +
" : "+ arr[i]);
}
}
Output of the above code:
Element at index 0 : 89
Element at index 1 : 12
Element at index 2 : 79
Element at index 3 : 20
Element at index 4 : 83
Element at index 5 : 42
Element at index 6 : 39
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java