Comparison of additions and deletions between tables of Java data Structures---ArrayList and linkedlist

Source: Internet
Author: User

First, the realization of the Java_collections table

Unlike C, Java has implemented and encapsulated ready-made table data structures, sequential tables, and linked lists.

1, ArrayList is based on the implementation of the array, so it has the characteristics of: 1. There are index values for easy lookups, and for get and set operations, it takes constant time, 2. But the downside is that inserting/deleting a data is expensive.

2, LinkedList is based on the double-linked list implementation, so has the characteristics are: 1. Easy to insert and delete based on linked list cost is small, 2. However, it is not easy to index, regardless of which element you want to start from scratch.

Add or remove operations here is a simple example: give a table (specifically to achieve the unknown), the table of the number of even values deleted, such as the Table element is: 1,2,3,4,5, the method after the call to get the element: 1,3,5

//-----------------------------------------------------------

This code is mainly used to illustrate the operation consumption of the sequential table and the linked list on additions and deletions.

1 Importjava.util.ArrayList;2 ImportJava.util.Iterator;3 Importjava.util.LinkedList;4 Importjava.util.List;5 ImportJava.util.ListIterator;6 7 /**8 * Find a table list of even values of the items, and delete the following shows the gradual optimization of the process comparison sequence table: ArrayList and LinkedList. Own grasp9  * Ten  * @authorAdministrator One  *  A  */ -  Public classRemoveevens { -      Public Static voidMain (string[] args) { thelist<integer> list =NewArraylist<>(); -List.add (1); -List.add (2); -List.add (3); +List.add (4); -  +         //removeEvensVer2 (list); A  at          for(inti:list) { - System.out.println (i); -         } -     } -  -     //Base Version in     /* - * For ArrayList collections, the Get method is faster, but the time to remove is two times. to * For LinkedList collection, exposing two problems first is that the call to get is not very efficient so the routine takes two times, and at the same time the remove + * The call is also inefficient because the cost to reach I is expensive.  -      */ the      Public Static voidRemoveEvensVer1 (list<integer>list) { *  $         inti = 0;Panax Notoginseng         //iterate through the list to get each element -          while(I <list.size ()) { the             //if the current item is an even number, remove +             if(List.get (i)% 2 = = 0) { A List.remove (i); the}Else { +i++; -             } $         } $     } -  -     //iterator version-enhanced version to avoid concurrent modification exceptions the     //the Remove method for the LinkedList iterator consumes only the constant time term, which is still incurable for ArrayList -     Wuyi      Public Static voidRemoveEvensVer2 (list<integer>list) { theIterator<integer> Iterator =list.iterator (); -          while(Iterator.hasnext ()) { Wu             if(Iterator.next ()% 2 = = 0) { - Iterator.remove (); About             } $         } -     } -  -     //Concurrency modification exceptions, which typically occur here, mainly due to invalid references to iterators A      Public Static voidRemoveEvensVer3 (list<integer>list) { +         //listiterator<integer> listiterator = List.listiterator (); the         //The enhanced for is essentially an iterator implementation, and the Remove method of list causes the list to change, further causing the iterator reference of the original list -         //changes, resulting in concurrency exceptions.  $          for(Integer x:list) { the             if(x% 2 = = 0) { the list.remove (x); the             } the         } -     } in}

Comparison of additions and deletions between tables of Java data Structures---ArrayList and linkedlist

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.