Traversal of list in "Go" Java

Source: Internet
Author: User

Original URL: http://blog.csdn.net/player26/article/details/3955906

  1. Import java.util.ArrayList;
  2. Import Java.util.Iterator;
  3. Import java.util.List;
  4. Public class Listtest {
  5. public static void Main (string[] args) {
  6. list<integer> list = new arraylist<integer> ();
  7. List.add (1);
  8. List.add (2);
  9. List.add (3);
  10. For (Iterator i = List.iterator (); I.hasnext ();)
  11. System. out.println (I.next ()); //Line 1
  12. }
  13. }
  14. Public class ListTest2 {
  15. public static void Main (string[] args) {
  16. List List = new ArrayList ();
  17. List.add (new Integer (100));
  18. List.add (new Float (150.60));
  19. List.add (new String ("abc"));
  20. For (Iterator i = List.iterator (); I.hasnext ();)
  21. System.  out.println (I.next ());
  22. list<integer> list = new arraylist<integer> ();
  23. List.add (1);
  24. List.add (2);
  25. List.add (3);
  26. for (Integer i:list) {
  27. System.out.println (i); Ok
  28. //         }
  29. }
  30. }
  31. Although the list's generic is an integer, the type returned by. Next () is an object
  32. Public class ListTest3 {
  33. public static void Main (string[] args) {
  34. //list<integer> List = new arraylist<integer> ();
  35. //List.add (1);
  36. //List.add (2);
  37. //List.add (3);
  38. //for (Iterator i = List.iterator (); I.hasnext ();) {
  39. //Integer integerref = (integer) i.next ();//line 1
  40. ////compile Error
  41. //System.out.println (INTEGERREF);//Line 2
  42. // }  
  43. list<integer> list = new arraylist<integer> ();
  44. List.add (1);
  45. List.add (2);
  46. List.add (3);
  47. For (iterator<integer> i = List.iterator (); I.hasnext ();) {  
  48. Integer integerref = I.next (); //Line 1
  49. //OK
  50. System. out.println (INTEGERREF); //Line 2
  51. }
  52. }
  53. }

There are three ways to traverse the list

list<a> list = new arraylist<a> ();
List.add (New A ());
List.add (New A ());
...

The first type:
for (iterator<a> it = List.iterator ();    It.hasnext (); )    {
....
}
This way in the loop

Data locking during execution, performance is slightly poor, at the same time, if you want to remove an element during the loop, you can only call the It.remove method, you cannot use the List.remove method, or you must have a concurrent access error.

The second type:
for (A a:list) {
.....
}
Internal call the first, in the bottle, this loop way there are other restrictions, do not recommend the use of it

The third type:
for (int i=0;    I<list.size (); i++) {
A = List.get (i);
...
}
Internal non-locking, the most efficient, but when writing multi-threading to consider the problem of concurrent operation!

Traversal of list in "Go" Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.