// Fig. 18.01: VectorTest.java // Testing the Vector class of the java.util package import java.util.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class VectorTest extends Applet implements ActionListener { private Vector v; // GUI components private TextField input; private Button addBtn, removeBtn, firstBtn, lastBtn, emptyBtn, containsBtn, locationBtn, trimBtn, statsBtn, displayBtn; public void init() { v = new Vector( 1 ); add( new Label( "Enter a string" ) ); input = new TextField( 10 ); add( input ); // value to add, remove or locate addBtn = new Button( "Add" ); addBtn.addActionListener( this ); add( addBtn ); // add the input value removeBtn = new Button( "Remove" ); removeBtn.addActionListener( this ); add( removeBtn ); // remove the input value firstBtn = new Button( "First" ); firstBtn.addActionListener( this ); add( firstBtn ); // look at the first element lastBtn = new Button( "Last" ); lastBtn.addActionListener( this ); add( lastBtn ); // look at the last element emptyBtn = new Button( "Is Empty?" ); emptyBtn.addActionListener( this ); add( emptyBtn ); // check if stack is empty containsBtn = new Button( "Contains" ); containsBtn.addActionListener( this ); add( containsBtn ); // does vector contain input value? locationBtn = new Button( "Location" ); locationBtn.addActionListener( this ); add( locationBtn ); // location of input value trimBtn = new Button( "Trim" ); trimBtn.addActionListener( this ); add( trimBtn ); // trim vector to number of elements statsBtn = new Button( "Statistics" ); statsBtn.addActionListener( this ); add( statsBtn ); // display statistics displayBtn = new Button( "Display" ); displayBtn.addActionListener( this ); add( displayBtn ); // display the stack contents } public void actionPerformed( ActionEvent e ) { if ( e.getSource() == addBtn ) { v.addElement( input.getText() ); showStatus( "Added to end: " + input.getText() ); } else if ( e.getSource() == removeBtn ) { if ( v.removeElement( input.getText() ) ) showStatus( "Removed: " + input.getText() ); else showStatus( input.getText() + " not in vector" ); } else if ( e.getSource() == firstBtn ) { try { showStatus( "First element: " + v.firstElement() ); } catch ( NoSuchElementException exception ) { showStatus( exception.toString() ); } } else if ( e.getSource() == lastBtn ) { try { showStatus( "Last element: " + v.lastElement() ); } catch ( NoSuchElementException exception ) { showStatus( exception.toString() ); } } else if ( e.getSource() == emptyBtn ) { showStatus( v.isEmpty() ? "Vector is empty" : "Vector is not empty" ); } else if ( e.getSource() == containsBtn ) { String searchKey = input.getText(); if ( v.contains( searchKey ) ) showStatus( "Vector contains " + searchKey ); else showStatus( "Vector does not contain " + searchKey ); } else if ( e.getSource() == locationBtn ) { showStatus( "Element is at location " + v.indexOf( input.getText() ) ); } else if ( e.getSource() == trimBtn ) { v.trimToSize(); showStatus( "Vector trimmed to size" ); } else if ( e.getSource() == statsBtn ) { showStatus( "Size = " + v.size() + "; capacity = " + v.capacity() ); } else if ( e.getSource() == displayBtn ) { Enumeration enum = v.elements(); StringBuffer buf = new StringBuffer(); while ( enum.hasMoreElements() ) buf.append( enum.nextElement() ).append( " " ); showStatus( buf.toString() ); } input.setText( "" ); } }