TestVector.java
Select all
/* TestVector.java CIS 260 2/6/2012 David Klick Demonstrates the Vector class. */ import java.util.Vector; public class TestVector { public static void main(String[] args) { Vector
v = new Vector
(); v.add(1); // same as addElement v.add(2); v.add(0, 3); System.out.println(v); if (v.contains(2)) { System.out.println("vector contains 2"); } else { System.out.println("vector does not contain 2"); } if (v.contains(4)) { System.out.println("vector contains 4"); } else { System.out.println("vector does not contain 4"); } System.out.println("The first element is " + v.firstElement()); System.out.println("v[1] = " + v.elementAt(1)); // same as get System.out.println("The last element is " + v.lastElement()); v.setElementAt(4, 0); v.set(2, 5); v.add(2); System.out.println(v); System.out.println("First 2 at: " + v.indexOf(2)); System.out.println("Last 2 at: " + v.lastIndexOf(2)); v.removeElement(2); v.removeElementAt(0); System.out.println(v); v.insertElementAt(7, 1); System.out.println(v); System.out.println("Size of v: " + v.size()); v.setSize(2); System.out.println("Size of v: " + v.size()); v.clear(); if (v.isEmpty()) System.out.println("v is now empty"); } } /* Sample output: [3, 1, 2] vector contains 2 vector does not contain 4 The first element is 3 v[1] = 1 The last element is 2 [4, 1, 5, 2] First 2 at: 3 Last 2 at: 3 [1, 5] [1, 7, 5] Size of v: 3 Size of v: 2 v is now empty */