The general differences between ArrayList and LinkedList are as follows:
1.ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on linked list.
2. For random access get and set,arraylist feel better than LinkedList, because linkedlist to move the pointer.
3. Add and Remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.
On the code:
1 static final int n=50000; 2 Static long Timelist (List list) {3 long Start=system.currenttimemillis (); 4 object o = new Object (); 5 for (int i=0;i<n;i++) {6 list.add (0, O); 7} 8 return System.currenttimemillis () -start; 9}10 Static long readlist (list list) {One long Start=system.currenttimemillis (); (int i=0,j=li St.size (); i<j;i++) {}15 return System.currenttimemillis ()-start;16}17 static List Addl IST (List List) {Object o = new Object (); (int i=0;i<n;i++) {list.add (0, O); 22 }23 return list;24}25 public static void Main (string[] args) {System.out.println ("Arrayli St adding "+n+" strips time-consuming: "+timelist (New ArrayList ())); System.out.println (" LinkedList adding "+n+" strips Time: "+timelist (new LinkedList () ); list list1=addlist (New arraylist<> ()); list list2=addlist (new linkedlist≪> ()); System.out.println ("ArrayList find" +n+ "Strip Time:" +readlist (List1)); System.out.println (LinkedList Finding "+n+" strips time-consuming: "+timelist (List2)); 33}
When we load 50,000 data in the collection, the test run results are as follows:
Obviously we can see that ArrayList is more suitable for reading data and LinkedList adding or deleting data more often.
ArrayList internally is implemented using an extensible array, so it takes constant time to use the get and set methods, but if you insert and delete elements, the code is expensive, unless the insertion and deletion positions are at the end of the table, because it requires the movement of the array.
LinkedList is implemented using a doubly linked list, so get is very resource intensive, unless the location is close to the head. However, inserting and deleting elements takes a constant amount of time.
ArrayList and LinkedList differences in Java