- Package com.sort;
- Import java.util.ArrayList;
- Import Java.util.Iterator;
- Import java.util.List;
- /**
- * List of three kinds of traversal
- *
- */
- Public class Listtest {
- public static void Main (string[] args) {
- list<string> list = new arraylist<string> ();
- List.add ("a");
- List.add ("B");
- List.add ("C");
- List.add ("C"); Can add duplicate data
- //Traversal method one
- For (iterator<string> Iterator = List.iterator (); Iterator.hasnext ();) {
- String value = Iterator.next ();
- System.out.println (value);
- }
- //Traversal method two
- For (String value:list) {
- System.out.println (value);
- }
- //Traverse method three
- For (int i=0;i<list.size (); i++) {
- System.out.println (List.get (i));
- }
- }
- }
Comparative analysis of three types of traversal:
Method One traversal:
Data locks are performed during execution, and performance is slightly worse, and you can only invoke the It.remove method if you want to remove an element from the loop.
Method two times:
Internal invocation of the first
Method Three traversal:
Internal not locked, the most efficient, but when writing multi-threading to consider the problem of concurrent operation
The two main implementations of the list interface, ArrayList, and LinkedList, can be used in such a way to traverse
A comparative analysis of ArrayList and LinkedList
A) ArrayList the bottom of the array implementation, linkedlist the bottom of the two-way linked list implementation.
b) When performing an insert or delete operation, it is better to use LinkedList.
c) When performing a search operation, it is better to use ArrayList.
To be blunt, sequential storage and chained storage in data structures
3 Ways to traverse the list collection