Address: http://www.java2000.net/p11848
List is a frequently-used data structure, which is more frequently used than map and set, because I often use it to return the database operation result set.
- Package collection. lession6;
- Import java. util. arraylist;
- Import java. util. iterator;
- Import java. util. Collections list;
- Import java. util. List;
- Import java. util. listiterator;
- Import java. util. Stack;
- Import java. util. vector;
- /**
- * Old zizhu Java tutorial (6)-list <br>
- * List can precisely control the sequence in the set. <Br>
- * Data can be accessed directly through the data location. <Br>
- * Repeated data is generally allowed <br>
- * Generally, null is allowed and multiple null values are allowed.
- *
- * @ Author old zizhu Java century network (java2000.net)
- *
- */
- Public class lession6 {
- Public static void main (string [] ARGs ){
- // The following are commonly used implementation classes in list
- // Implement a variable-size list, allowing any data to be added
- Arraylist = new arraylist ();
- // It is also the implementation of a variable-size list.
- // The biggest difference from arraylist is that the method of this class is synchronous (synchronized ).
- // For detailed differences, see
- // Check the source code to understand the real difference between arraylist and vector.
- // Http://www.java2000.net/p9750
- Vector vector = new vector ();
- // Implements bidirectional queue control, including operations at the beginning and end
- // Can be used for Stack and FIFO operations.
- Listparts list = new parts list ();
- // Integrate the self-Vector
- // Provides the LIFO stack operation method.
- Stack stack = new stack ();
- //-----------------
- // The following uses arraylist as an example to check the methods in the list.
- List list = new arraylist ();
- // Construct the second list
- List list2 = new arraylist ();
- // Add the specified data to the end of the list
- List. Add (1, 123 );
- // Insert the specified data at the specified position in the list
- // Insert data at the beginning
- // The index at the position of the list starts from 0.
- List. Add (1, 0,456 );
- // Put all the data in list2 to the end of list1.
- // Note that this can be any collection class, not limited to list
- List. addall (list2 );
- // Put all the data in list2 to the end of the specified position in list1.
- // Insert a piece of data
- List. addall (2, list2 );
- // Clear the list and delete all the data in it
- List. Clear ();
- // Determine whether the list contains data
- Boolean found = List. Contains (123 );
- // Determine whether the list contains all the data of another set
- // Note that this can be any collection class, not limited to list
- Boolean OK = List. containsall (list2 );
- // Obtain data at the specified position
- // If the position exceeds the range of list 0 to list. Size ()-1
- // Throw an exception: indexoutofboundsexception
- Object OBJ = list. Get (3 );
- // Obtain the position of the data in the list
- // If not found,-1 is returned.
- // The Position Index starts from 0.
- Int Index = List. indexof (456 );
- // Check whether the list is empty
- Boolean empty = List. isempty ();
- // List iterator operation
- // The Order is strictly in the order of list Storage
- Iterator it = List. iterator ();
- // Obtain the position where the data is last displayed in the list
- // Suitable for the list with duplicate data
- Index = List. lastindexof (456 );
- // Another iterator in the list
- Listiterator listit = List. listiterator ();
- // Another iterator In the list. You can specify the start position.
- Listiterator listit2 = List. listiterator (3 );
- // Delete data at a certain position
- List. Remove (3 );
- // Delete the first location where the specified object appears
- // Note: if it is an integer, you must distinguish it from remove (INT ).
- // We recommend that you use remove (New INTEGER (123); to delete data objects.
- List. Remove (New INTEGER (123 ));
- // Delete the data in another set in the list
- List. removeall (list2 );
- // Only the data in another set is retained, equal to the intersection
- List. retainall (list2 );
- // Replace the data at the specified position
- List. Set (3,999 );
- // Number of List Data
- Int size = List. Size ();
- // Obtain a subarray
- List sublist = List. sublist (2, 10 );
- // Convert the set into an array
- Object [] objs = List. toarray ();
- // Convert the set to an array in the specified format
- // For example, strings are saved in the set.
- String [] objs2 = (string []) List. toarray (New String [0]);
- }
- }
Summary:
In fact, we usually use arraylist. Because vector is a synchronous method, it is used only when multiple threads and global data are involved.