import java.util.*;
import java.io.*;

public class VectTest {

    public static void main (String [] args)  throws IOException {
	// Create a buffered reader
	BufferedReader in = new BufferedReader(
			new InputStreamReader(System.in));
	// string for storing a line of input
	String line;
	
	// create a new vector
	Vector v = new Vector();

	do {
	    System.out.print("Please enter a string, \"bye\" to exit: ");
	    line = in.readLine();
	    v.add(line);
	} while ( !line.equals("bye") );

	// remove the string "bye"
	v.remove(v.size() - 1);
	
	// printing out the strings:
	int size = v.size();
	for (int i = 0; i < size; ++i) {
	    System.out.println("at index " + + i + ": " + v.elementAt(i)); 
	}

	// printing strings in upper case
	// need typecasting before we can call the method toUpperCase()
	for (int i = 0; i < size; ++i) {
	    System.out.println("at index " + + i + ": " + 
			       ((String) v.elementAt(i)).toUpperCase()); 
	}	

	// move the vector to an array
	String [] str = new String [v.size()];
	str = (String []) v.toArray( str);

	// check the array
        for (int i = 0; i < str.length; ++i) {
	    System.out.println("str[" + i + "] = " + str[i]); 
	}	
    }

}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.