1. First create a list collection and add data to it
1 List list = newarraylist (); 2 3 List.add ("1"); 4 5 List.add ("2"); 6 7 List.add ("3"); 8 9 List.add ("2"); // can add duplicate data
2. Traverse the collection
Method One:
1 for (Iterator Iterator = list.iterator (); Iterator.hasnext ();)2{3 String value = Iterator.next (); 4 5 }
Method Two:
1 fot (String value:list) 2 {3 4 }
Method Three:
1 for (I=0;i<list.size (); i++) 2 {3 4 }
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.
Summary: ArrayList principle is the sequential storage in the data structure, The linkedlist principle is Chain-Store Storage
How to implement the traversal of a list collection in Java