Vector in java collection
Program of vector in java collection framework
Vector class implements a growable array of object means vector can increase their size.
Program//code
package com.vector;
import java.util.Vector;
//The Vector class implements a growable array of objects.
public class Vector1 {
public static void main(String[] args) {
//creating vector
Vector<Integer> v = new Vector<>();
//add element
v.addElement(34);
v.addElement(33);
v.addElement(31);
System.out.println(v);
//insert element at index 1
v.insertElementAt(12, 2);
System.out.println("adding 12 at index 2:"+v);
//remove element
v.removeElementAt(1);
System.out.println("Remove element from index 1:"+v);
//print all with for loop
for(int j : v) {
System.out.print(j+" ");
}
}
}
//Output
[34, 33, 31]
adding 12 at index 2:[34, 33, 12, 31]
Remove element from index 1:[34, 12, 31]
34 12 31
Comments
Post a Comment