Java PR 14

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 2

Program:

import java.util.Vector;

public class VectorEx1 {


public static void main(String[] args) {
Vector v = new Vector(5, 2);
System.out.println("Initial Cappacity: " + v.capacity());
System.out.println("Initial Size: " + v.size());
v.add(new Integer(19));
v.addElement(new Float(3.4f));
v.addElement(new Double(34.67));
v.add("Vector is dynamic");
v.add(new Integer(56));
v.add(new Character('c'));
v.add(3.33);
System.out.println("Capacity: " + v.capacity());
System.out.println("Size: " + v.size());
System.out.println("Elements of Vector: " + v);
}
}

Output:
Initial Cappacity: 5
Initial Size: 0
Capacity: 7
Size: 7
Elements of Vector: [19, 3.4, 34.67, Vector is dynamic, 56, c, 3.33]

Program:
import java.util.Vector;

public class VectorEx2 {


public static void main(String[] args) {
Vector v = new Vector(4, 2);
System.out.println("Initial cpapacity: " + v.capacity());
System.out.println("Initial size: " + v.size());
v.addElement(new Integer(28));
v.addElement(new Float("3.14"));
v.addElement(new String("Vectors are dynamic"));
v.insertElementAt(70, 1);
v.insertElementAt('j', 0);

System.out.println("First Element: " + v.firstElement());


System.out.println("Last Element: " + v.lastElement());
System.out.println("Element at index 4: " + v.elementAt(4));
System.out.println("Index of 28: " + v.indexOf(28));

}
}

Output:
Initial cpapacity: 4
Initial size: 0
First Element: j
Last Element: Vectors are dynamic
Element at index 4: Vectors are dynamic
Index of 28: 1

You might also like