/* Lesley Hogan * June 30th 2006 * VectorUsage.java * Manipulate the Vector class * CS 101 HW J2: * http://www.cs.virginia.edu/~asb/teaching/cs101-spring06/hws/hwj2/index.html */ package net.bunnie.uva.cs101; import java.util.*; public class VectorUsage { public static void main (String[] args) { System.out.println("Let's make a vector of some movies."); //declarations Vector v = new Vector(); printVector(v); Scanner s = new Scanner(System.in); int i; final int NUMBER_OF_ENTRIES = 5; String searchValue; String getString; //create vector for (i = 0; i < NUMBER_OF_ENTRIES; i++) { System.out.print("Enter movie #" + (i + 1) + ": "); v.add(s.next()); } printVector(v); //search System.out.println("What is your favorite movie?"); System.out.print("#1: "); searchValue = s.next(); i = v.indexOf(searchValue); if (i == 0) { //matches 1st movie System.out.println("That was the first movie you entered!"); } else if (i > 0) { //matches another movie System.out.println("Then how come you entered " + searchValue + " in the #" + (i + 1) + " spot?"); } else { // -1 System.out.println(searchValue + " wasn't even on the list of movies you entered!"); } //first/last getString = v.firstElement(); System.out.println("The movie you entered first: " + getString); getString = v.lastElement(); System.out.println("The movie you entered last: " + getString); //remove System.out.println("Pick a movie by its index number (1-" + v.size() + ")"); i = s.nextInt() - 1; getString = v.get(i); System.out.println("You picked movie #" + (i + 1) + ": " + getString); v.remove(i); System.out.println("This movie has been selected for removal."); printVector(v); //change an element System.out.print("What's your least favorite movie? "); getString = s.next(); System.out.print("How many times have you seen it (1-" + v.size() + "max): "); i = s.nextInt() - 1; v.set(i, getString); System.out.println("Haha, I put it in the #" + (i + 1) + " spot!"); printVector(v); System.out.println("Okay, enough of this. Bye bye, vector!"); v.clear(); printVector(v); } public static void printVector(Vector v) { System.out.println ("The Vector has size " + v.size() + ", and contains the following elements: " + v); } }