Java basics-ArrayList and rule list (2), arraylistparts list
Exercise ArrayList and rule list today. You can see questions about the application efficiency on the Internet. I think it is very valuable. Keep it for a moment.
1 import java. util. arrayList; 2 import java. util. using list; 3 import java. util. list; 4 5 public class CollT5 {6 static final int N = 50000; 7 8 static long timeList (List <Object> list) {9 10 long start = System. currentTimeMillis (); 11 12 Object o = new Object (); 13 for (int I = 0; I <N; I ++) 14 list. add (0, o); 15 return System. currentTimeMillis ()-start; 16} 17 18 public static void main (String [] args) {19 System. out. println ("ArrayList time consumed:" + timeList (new ArrayList <Object> (); 20 System. out. println ("Upload list time consumed:" + timeList (new upload list <Object> (); 21} 22}
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.LinkedList; 4 import java.util.List; 5 6 public class CollT6 { 7 static List<Integer> array = new ArrayList<Integer>(); 8 static List<Integer> linked = new LinkedList<Integer>(); 9 10 public static void main(String[] args) {11 12 for (int i = 0; i < 10000; i++) {13 array.add(i);14 linked.add(i);15 }16 System.out.println("array time:" + getTime(array));17 System.out.println("linked time:" + getTime(linked));18 System.out.println("array insert time:" + insertTime(array));19 System.out.println("linked insert time:" + insertTime(linked));20 21 }22 23 public static long getTime(@SuppressWarnings("rawtypes") List list) {24 long time = System.currentTimeMillis();25 for (int i = 0; i < 10000; i++) {26 @SuppressWarnings("unchecked")27 int index = Collections.binarySearch(list, list.get(i));28 if (index != i) {29 System.out.println("ERROR!");30 }31 }32 return System.currentTimeMillis() - time;33 }34 35 @SuppressWarnings("unchecked")36 public static long insertTime(@SuppressWarnings("rawtypes") List list) {37 long time = System.currentTimeMillis();38 for (int i = 100; i < 10000; i++) {39 list.add(5000, i);40 }41 return System.currentTimeMillis() - time;42 43 }44 45 }
Additional:
When there are three points behind the java type that are representative, let's add:
1 // The following three vertices of the java type indicate that multiple actual parameters can be accepted. Multiple parameters here refer to unlimited numbers, it can be one, two, three, or more. 2 // Add three vertices after the type in java to the new content that appears after java1.5. 3 // It is used on the form parameter of the function, which is equivalent to an array. The actual parameters passed when the function is called can be stored on this form parameter. 4 // note that this parameter must be placed on the last parameter; otherwise, an error is returned! 5 // variable array parameters. If a parameter is used, it can be passed or not. If it is passed, it can be passed in parallel or directly an array. 6 // In the method, s is an array. If nothing is passed during the call, the length of s is 0. The length of s is a few. 7 public class CollT4 {8 public static void main (String [] args) {9 printString (); 10 System. out. println ("==========="); 11 printString (new String [] {"I", "and", "you "}); 12 System. out. println ("==========="); 13 printString ("I", "and", "you "); 14} 15 16 public static void printString (String... str) {17 if (str. length = 0) {18 System. out. println ("No parameter is passed. "); 19} 20 for (int I = 0; I <str. length; I ++) {21 System. out. println (str [I]); 22} 23} 24}
Reference Links:
Http://pengcqu.iteye.com/blog/502676