VectorListOps.java
// This code example is from the following source: // // Book Title: Programming with Objects, A Comparative Presentation // of Object-Oriented Programming with C++ and Java // // Chapter: Chapter 5 ---- Using the Container Classes // // Section: Section 5.2.4 ---- vector // // The links to the rest of the code in this book are at // // http://programming-with-objects.com/pwocode.html // // For further information regarding the book, please visit // // http://programming-with-objects.com // //VectorListOps.java import java.io.*; import java.util.*; class VectorListOps { public static void main( String[] args ) { Vector charVec = new Vector(); //(A) charVec.addElement( new Character( 'c' ) ); //(B) charVec.addElement( new Character( 'a' ) ); //(C) charVec.addElement( new Character( 't' ) ); //(D) charVec.insertElementAt(new Character('h'), 1); // chat //(E) charVec.removeElementAt( 0 ); // hat //(F) charVec.addElement( new Character( 's' ) ); // hats //(G) charVec.removeElement( new Character( 't' ) ); // has //(H) System.out.println( charVec.size() ); // 3 char[] charArray = new char[charVec.size()]; for ( int i=0; i<charVec.size(); i++ ) { Character Ch = (Character) charVec.elementAt(i); charArray[i] = Ch.charValue(); } String str = new String( charArray ); System.out.println( str ); // has } }
C:\classes\ece538\work\kak05>javac VectorListOps.java Note: VectorListOps.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. C:\classes\ece538\work\kak05>javac -Xlint VectorListOps.java VectorListOps.java:31: warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector charVec.addElement( new Character( 'c' ) ); //(B) ^ VectorListOps.java:32: warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector charVec.addElement( new Character( 'a' ) ); //(C) ^ VectorListOps.java:33: warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector charVec.addElement( new Character( 't' ) ); //(D) ^ VectorListOps.java:35: warning: [unchecked] unchecked call to insertElementAt(E, int) as a member of the raw type java.util.Vector charVec.insertElementAt(new Character('h'), 1); // chat //(E) ^ VectorListOps.java:37: warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector charVec.addElement( new Character( 's' ) ); // hats //(G) ^ 5 warnings C:\classes\ece538\work\kak05>java VectorListOps 3 has
Maintained by John Loomis, updated Sat Jan 06 21:59:51 2007