The example of this article tells about Java's traversal and efficiency comparison with ArrayList. Share it for everyone for your reference. The specifics are as follows:
ArrayList arrl = new arrayList (); arrayList arrltmp1 = new arrayList (); arrayList arrestmp2 = new arrayList (); (); ArrayList arrltmp4 = New ArrayList (); for (int i = 0; i <1000000 ; i ++) {arrl.add ("" "+I+" one ");} Long t1 = system.nanotime (); // Method 1Item it = arrl.itrator (); space (iT.hasnext ()) {arrltmp11 .add (it.next ());} Long t2 = System.nanotime (); // Method 2FOR (ITERTOR it2 = Arrl.itrator (); IT2.hasnext ();) );} Long t3 = System.nanotime (); // Method 3FOR (String vv: Arrl) {arrLTMP3.add (vv);} Long T4 = System.nanotime (); // Method 4FOR (int i = 0 0 0 0 ; IRRLTMP4.add (Arrl.get (i));} Long T5 = System.nanotime (); System.out.println ("The first method time consuming:" + (T2-T1)/1000 + "micro Second "); System.out.println (" The second method is time-consuming: " + (T3-T2)/1000 +" microsecond "); System.out.println (" The third method time consuming: " + + (T4-T3)/1000 + "microsecond"); System.out.println ("The fourth method time:" + (T5-T4)/1000 + "microsecond");
Output results:
The first method is time -consuming: 143069 microseconds The second method takes time: 381666 microseconds The third method takes time: 125909 Microsecond
Change the above 100000 to 10, and the output result is:
The first method is time -consuming: 307 microseconds The second method consuming: 14 microseconds The third method takes time: 14 microseconds The fourth method consuming: 14 microseconds
It is hoped that this article is helpful to everyone's Java program design.