Practice ArrayList and LinkedList today to see the questions about the efficiency of their applications on the Internet. Feel very valuable, keep it.
1 Importjava.util.ArrayList;2 Importjava.util.LinkedList;3 Importjava.util.List;4 5 Public classCollT5 {6 Static Final intN = 50000;7 8 Static LongTimelist (list<object>list) {9 Ten LongStart =System.currenttimemillis (); One AObject o =NewObject (); - for(inti = 0; i < N; i++) -List.add (0, O); the returnSystem.currenttimemillis ()-start; - } - - Public Static voidMain (string[] args) { +System.out.println ("ArrayList time:" + timelist (NewArraylist<object>())); -System.out.println ("LinkedList time:" + timelist (NewLinkedlist<object>())); + } A}
1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 Importjava.util.LinkedList;4 Importjava.util.List;5 6 Public classCollT6 {7 Staticlist<integer> array =NewArraylist<integer>();8 Staticlist<integer> linked =NewLinkedlist<integer>();9 Ten Public Static voidMain (string[] args) { One A for(inti = 0; I < 10000; i++) { - Array.add (i); - Linked.add (i); the } -SYSTEM.OUT.PRINTLN ("Array Time:" +getTime (array)); -SYSTEM.OUT.PRINTLN ("Linked time:" +GetTime (linked)); -SYSTEM.OUT.PRINTLN ("Array Insert time:" +inserttime (array)); +SYSTEM.OUT.PRINTLN ("Linked Insert time:" +Inserttime (linked)); - + } A at Public Static LongGetTime (@SuppressWarnings ("Rawtypes") (List list) { - LongTime =System.currenttimemillis (); - for(inti = 0; I < 10000; i++) { -@SuppressWarnings ("Unchecked") - intindex =collections.binarysearch (list, list.get (i)); - if(Index! =i) { inSystem.out.println ("error!"); - } to } + returnSystem.currenttimemillis ()-Time ; - } the *@SuppressWarnings ("Unchecked") $ Public Static LongInserttime (@SuppressWarnings ("Rawtypes") (List list) {Panax Notoginseng LongTime =System.currenttimemillis (); - for(inti = 100; I < 10000; i++) { theList.add (5000, i); + } A returnSystem.currenttimemillis ()-Time ; the + } - $}
Additional:
After encountering the Java type followed by three points is the case, add:
1 //The Java type followed by three points is the representative can accept more than the actual parameters, here the number refers to an unlimited number, can be one, two, three or more. 2 //The addition of three points after the type in Java is the new content that appears after java1.5.3 //used on a function's formal parameter, the equivalent of an array, the invocation of the function when the actual parameters are passed, can be stored on this parameter.4 //It is important to note that this parameter must be placed on the last parameter position, otherwise it will be an error! 5 //that is, the variable group parameter. Use... , the transmission of parameters can not be transmitted, the word, can be a side by side to pass, can also be directly an array. 6 //when taken in a method, S is an array, and if nothing is passed at the time of the call, the length of S is 0. Pass the time to pass a few, s of length is a few. 7 Public classCollT4 {8 Public Static voidMain (string[] args) {9 printstring ();TenSystem.out.println ("=========="); OnePrintstring (NewString[] {"I", "and", "You" }); ASystem.out.println ("=========="); -Printstring ("I", "and", "You"); - } the - Public Static voidprintstring (String ... str) { - if(Str.length = = 0) { -System.out.println ("No arguments are passed. "); + } - for(inti = 0; i < str.length; i++) { + System.out.println (Str[i]); A } at } -}
Related Reference Links:
http://pengcqu.iteye.com/blog/502676
Java Foundation--arraylist and LinkedList (ii)