Source code: Java Collection Source: Array and list (i)

Source: Internet
Author: User

Arrays and linked lists are the most basic parts of data structures.

Array

In Java, an array is defined as a primitive type that can be used to obtain data at the corresponding location. So how does this structure of data be stored in memory?

An array is a contiguous storage unit in memory, with each data sequentially placed in each cell.

Analyzing this structure, we can draw the following conclusions:

    • To create an array, you must declare its length to find a suitable contiguous storage unit in memory. This also means that the size of the arrays is fixed and we cannot dynamically resize them.
    • To get the element I in the array, the time complexity is O (1), because it can be found directly based on its address. The same is true of modification.
    • Arrays are generally performed on queries, and to find an element, a traversal is required, and the time complexity is O (n).
    • Because the address is contiguous, it is complicated to want to insert an element into the array, since all elements behind it need to be moved backwards from the insertion position. Similarly delete is also, just move direction for forward. Also, when the array is full, it cannot continue to be inserted.
    • Because the array occupies an entire block of memory, it is possible to produce a lot of fragments, or the storage fails because a suitable block of memory cannot be found.

Summed up is: The array size fixed, Find Fast, delete complex, need a complete memory block, easy to produce fragments.

Linked list

A linked list is a discrete storage structure that is stored in memory that is not contiguous, with each data element pointing to the address of its next element through a pointer. Depending on the field of the pointer, the list is divided into single-linked lists, doubly linked lists, circular linked lists, and so on, where we only analyze single-linked lists. As shown below:

Analyzing this structure, we can draw the following conclusions:

    • When declaring a linked list, you do not need to know its length or the contiguous block of memory, so its size can be dynamically adjusted.
    • Each element of a list is divided into a data field and a pointer field, which is the actual stored data, and the latter points to the address of the next element. Each element needs to occupy more memory than the array.
    • To get the first element of a linked list becomes complex, because its address is stored in the pointer field of its previous element, so I can only operate from the first element. The same is true of modification.
    • The list of queries is also generally performed, requiring traversal, and the time complexity is O (n).
    • It is more convenient to add and delete an element, because there is no limit to the memory address, we just need to properly handle the value of the pointer field at the corresponding node, we can insert an element into the linked list or delete from the list.
    • The list of memory requirements is small, as long as the memory block that can store the next data element can be used, and therefore does not cause fragmentation.

Summed up is: The size can be dynamically adjusted, delete quickly, find slower, data elements accounted for a little more memory, do not need the whole block of memory, will not cause fragmentation.

Selection of arrays and linked lists

From the above analysis, the difference between the array and the linked list affects us the most:

    • The array is quickly found by location, and the linked list is easy to delete
    • Arrays are fixed size and linked lists can be expanded and scaled down at any time
    • Linked list each element occupies slightly more memory than the array
    • Arrays and linked lists are generally more common in queries, and take a long time to perform.

When the amount of data is very small and the content is basically fixed, the impact of what data structure we choose is not very good.

But when the amount of data is large, if we need to make frequent insertions and deletions of the data, we should choose the linked list, if we need to get the element of a position frequently, we should select the array.

Arrays and linked lists do not have clear advantages and disadvantages, depending on the use of different scenarios to make different choices is the best way to use these two structures.

Source code: Java Collection Source: Array and list (i)

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.