1. Vector- specific features :
(1) Add function
public void addelement (Object obj)--add ()
(2) Get function
Public Object elementat (int index)--get ()
Public enumeration elements ()--iterator Iterator ()
Enumeration (Interface):
Boolean hasmoreelements ()--Hasnext ()
Object nextelement ()--Next ()
2. code example:
1 Packagecn.itcast_02;2 3 Importjava.util.Enumeration;4 ImportJava.util.Vector;5 6 /*7 * Unique features of vector:8 * 1: Add features9 * public void addelement (Object obj)--Add ()Ten * 2: Get features One * Public Object elementat (int index)--get () A * Public enumeration elements ()--Iterator Iterator () - * Boolean hasmoreelements () Hasnext () - * Object nextelement () Next () the * - * Reasons for JDK Upgrade: - * A: Security - * B: Efficiency + * C: Simplified writing - */ + Public classVectordemo { A Public Static voidMain (string[] args) { at //To create a collection object -Vector v =NewVector (); - - //Add features -V.addelement ("Hello"); -V.addelement ("World"); inV.addelement ("Java"); - to //Traverse + for(intx = 0; x < V.size (); X + +) { -String s =(String) v.elementat (x); the System.out.println (s); * } $ Panax NotoginsengSystem.out.println ("------------------"); - theEnumeration en = V.elements ();//returns an object of the implementation class + while(En.hasmoreelements ()) { AString s =(String) en.nextelement (); the System.out.println (s); + } - } $}
The results are as follows:
Java basic Knowledge Enhancement collection Frame Note 25:vector's unique features