Arraylist and rule list

Source: Internet
Author: User

For processing a column of data items, Java provides two classes: arraylist and arraylist. The internal implementation of arraylist is based on the internal array object []. So Conceptually, it is more like an array, however, the internal implementation of The shortlist is based on a set of connected records. Therefore, it is more like a linked list structure. Therefore, they differ greatly in performance.

When inserting data in front of or in the middle of the arraylist, all the data after it must be moved back. This will inevitably take a lot of time. Therefore, when you add data after a column of data instead of adding data in front or in the middle and randomly access the elements in the column, using arraylist provides better performance;

When accessing an element in the linked list, you must start from the end of the linked list and search for the element one by one in the connection direction until you find the required element. Therefore, when you add or delete data in front or middle of a column of data and access the elements in the column in sequence, you should use the sort list.

In programming, the two situations alternate. In this case, you can consider using a general interface such as list, instead of concerning the specific implementation. In specific circumstances, its performance is guaranteed by the specific implementation.

Case: Implementation stack of the consumer list

Case Study

Arraylist has a relatively high query efficiency and a relatively low efficiency of adding and deleting actions. It is suitable for a set of Element Management Functions with frequent queries and fewer addition and deletion actions. The query efficiency of the shortlist is low, but the addition and deletion efficiency is high. It is applicable to Element Management sets that are frequently added or deleted and have a small number of queries.

Both arraylist and worker list are thread-insecure.

Implement stack 1) array (arraylist, Which is inefficient at adding, deleting, and not suitable)

2) Implement list (a good way to implement stack)

3) Java. util. stack class: Stack is a subclass of vector. The Vector class is a thread-safe (a heavyweight class) and inherits the vector method. The verctor class and arraylist have almost the same functions. (Stack class is not recommended for stack implementation ).

Implementation Process

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.