A collection of Java basics

Source: Internet
Author: User

The difference between ArrayList and LinkedList

1.ArrayList is based on an array, LinkedList is based on a linked list implementation.

For ArrayList and LinkedList, the overhead of adding an element at the end of the list is fixed.

For ArrayList, the main point is to add an entry to the inner array, pointing to the added element, which may occasionally cause the array to be reassigned;

For LinkedList, this overhead is uniform and allocates an internal entry object.

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.

Inserting or deleting an element in the middle of a ArrayList means that the remaining elements in the list will be moved;

The overhead of inserting or deleting an element in the middle of a linkedlist is fixed.

4. find operation Indexof,lastindexof,contains and so on, both similar.

5.ArrayList space waste is mainly reflected in the list at the end of a certain amount of space reserved, and the linkedlist of the space cost is reflected in each of its elements need to consume considerable space

This is only a theoretical analysis, in fact, not necessarily, such as ArrayList at the end of the insertion and deletion of data is not designed to move data, but there is a suggestion:LinkedList does not support efficient random element access , Random access more than the words must be used ArrayList instead of LinkedList, if you need frequent insertions and deletions should consider using LinkedList to improve performance.

6. About expansion

The initial capacity of the Arraylis is 10,

When the capacity is not enough

Re-open a space of 1.5 times times +1

Copy the data.

It can be said that using ArrayList provides better performance when the action is to add data after a column of data rather than in front or in the middle, and to randomly access its elements, and when your action is to add or delete data in front or in the middle of a column of data, and to access its elements sequentially, You should use the LinkedList. ArrayList

Collection of Java Basics

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.