8.3 The Vector class |
The following piece of code demonstrates how to use a Vector object.
Vector aList = new Vector(); aList.addElement("Hello"); aList.addElement("There"); aList.addElement("This"); aList.addElement("Is a Test"); for (int loop=0; loop < aList.size(); loop++) System.out.println( (String) aList.elementAt(loop) ); // note type castThe addElement() accepts a Object class. The size() method returns the number of elements that are in the list. The elementAt() method returns the element at the specified position in the Vector the element is returned as a Object so you need to type cast it to the correct type.