This article first introduces the enumeration method unique to the Vector, which is the same as the iteration method, and then introduces the differences between the vector and the list.
Vector usage
Enumeration is a unique way to retrieve a Vector. enumeration is the same as iteration.
The enumeration name and method name are too long. Therefore, it is replaced by the iterator.
There are three methods to retrieve a set element:
Iteration, traversal, for Loop
Use enumeration:
The Code is as follows: |
Copy code |
Package com. day14.wd; Import java. util. Enumeration; Import java. util. Vector;
Public class VectorDemo { Public static void main (String [] args ){ Vector vec = new Vector (); Vec. add ("java01 "); Vec. add ("java02 "); Vec. add ("java03 "); Enumeration enu = vec. elements (); While (enu. hasMoreElements ()){ Object object = (Object) enu. nextElement (); System. out. println (object );
} } } |
List usage
Common methods and functions defined by the List Interface
From table 1, we can see that the common methods provided by the List interface are related to indexes. This is because the List set is of the List type and objects are stored linearly, you can use the index of an object to operate an object.
Common Implementation classes of the List interface include ArrayList and listlist. When using the List set, it is usually declared as the List type. During instantiation, It is instantiated as the ArrayList or listlist according to the actual needs, for example:
The Code is as follows: |
Copy code |
List <String> l = new ArrayList <String> (); // use the ArrayList class to instantiate a List set. List <String> l2 = new consumer List <String> (); // use the consumer List class to instantiate a List set.
|
1. Differences between the add (int index, Object obj) method and the set (int index, Object obj) Method
When using the List set, note that the add (int index, Object obj) method and set (int index, Object obj) method are distinguished. The former is to add an Object to the specified index location, the latter is to modify the object at the specified index location. For example, execute the following code:
Key code of srccommwqTestCollection. java:
The Code is as follows: |
Copy code |
Public static void main (String [] args ){ String a = "A", B = "B", c = "C", d = "D", e = "E "; List <String> list = new partition List <String> (); List. add (); List. add (e ); List. add (d ); List. set (1, B); // Modify Object e with index position 1 to object B. List. add (2, c); // add Object c to the location where the index is 2 Iterator <String> it = list. iterator (); While (it. hasNext ()){ System. out. println (it. next ()); } } The following information is output in the console: A B C D
|
Because the List set can access objects through the index location, you can also traverse the List set through the for loop. for example, the Code for traversing the List set in the above Code is as follows:
Key code of srccommwqTestCollection. java:
The Code is as follows: |
Copy code |
For (int I = 0; I <list. size (); I ++ ){ System. out. println (list. get (I); // use the get (int index) method to obtain the object at the specified index location } The complete code of srccommwqTestCollection. java is as follows: Package com. mwq; Import java. util. ArrayList; Import java. util. Collections list; Import java. util. Iterator; Import java. util. List; Public class TestCollection { Public static void main (String [] args ){ System. out. println ("START :"); String a = "A", B = "B", c = "C", d = "D", e = "E "; List <String> list = new partition List <String> (); List. add (); List. add (e ); List. add (d ); List. set (1, B); // Modify Object e with index position 1 to object B. List. add (2, c); // add Object c to the location where the index is 2 Iterator <String> it = list. iterator (); While (it. hasNext ()){ System. out. println (it. next ()); } // For (int I = 0; I <list. size (); I ++ ){ // System. out. println (list. get (I); // use the get (int index) method to obtain the object at the specified index location //} System. out. println ("end! "); } }
|
Differences between Vector and List
If you notice the Java versions supported by Vector and List, you should be able to find the answer. Java supports since 1.0 for Vector and since 1.2 for List. Sun made a lot of changes to java APIs between the two versions. One of the refactoring versions proposed the so-called Collection FrameWork, and the List was replaced by introduced at that time, it fully complies with the collection framework of version 1.2, while the Vector exists before the emergence of the Colleciton framework, but the java api does not change the Vector to Deprecated, mainly because of the backward compatiable problem, in the end, JCP refactoring the vector to conform to the customized Collection framework. In addition, the difference between Hashtable and HashMap is the same.
Conclusion: Use List, HashMap, rather than Vector & Hashtable.