From the source to understand ArrayList and LinkedList differences

Source: Internet
Author: User

Understand ArrayList and linkedlist differences from source code

ArrayList

ArrayList Default Capacity is ten, the essence is an array to hold the element,size represents ArrayList The number of elements that are contained.

ArrayList 3 Constructors, the first constructor specifies the capacity, the second constructor defaults to an empty array, and the third constructor changes from set C to array of Arraylist


Add function, first confirm that the array capacity is not enough, at first if the default initialization (that is, call the second constructor), add the element when the grow out of the array capacity of ten, Construct a new array and copy The value of the original array element, then element[0] assignment, the second time the element is added because elementdata! = Empty_ Elementdata and

Mincapacity-elementdata.length <0 so direct element[1] assignment, and does not expand capacity, only when full capacity will be expanded.

The Remove (int ) function, which moves the elementwith the ordinal index ,nummoved refers to the number of elements after index , The element behind the system.arraycopy pushes forward one cell , and the last element is null, returning the deleted value

the same principle as remove (int) above .


Returns the ordinal if it is found, otherwise returns -1

LinkedList

LinkedList Storage data is stored in the form of a chain list


first is pointing to the top node and last is pointing to the tail junction.



LinkedList has two constructors, and the second constructor passes the elements of the collection to the linked list



is a way to insert data using a linked list


Are all methods of deleting elements using a linked list


The method by which the linked list looks for elements.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

From the source to understand ArrayList and LinkedList differences

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.