List analysis of the JDK

Source: Internet
Author: User

List in the normal development of the use of a lot, but is generally oriented interface programming, so the use of the list type, but are used ArrayList or LinkedList related operations. This article mainly explains the ArrayList and LinkedList of the JDK source code.

    ArrayList is used in the form of array maintenance, the main methods are Add,remove,size,contains,toarray and other related methods. Add is actually very simple, the bottom layer is to increase the length of the array, and then put the elements into the array. It is implemented in the following way: this.elementdata[(this.size++)] = ParamE, before this operation, will increase the length of the array by 1, in order to put the newly added elements into the current array, but also to achieve the added function of the list. However, if you want to insert into the specified location, the operation is more complex. It moves the current array backwards by 1 bits, using the elements that follow the system.arraycopy copy, and then inserting the specified elements. Such time is the price. Therefore, in the case of inserting more elements, priority to use LinkedList, will be introduced later. The same is true of the remove principle. However, remove has an overload. When an int is subscript, it copies the specified element and then sets the element that specifies the subscript to null, which enables the ability to specify subscript deletion. But the overloads are not the same. He receives the object type so that remove can be done regardless of what type is stored in the list. When you use this method, it searches for the element that meets the criteria, takes the subscript, and then uses the Firstremove method to remove the specified element. Of course, this method can only remove the first element, if you want to remove all the matching elements, you have to traverse, or use the RemoveAll method. It is important to note that the RemoveAll receive parameter is collection. As for the size of the ArrayList is very simple, because the bottom is an array, so only need to return the current array of sizes is the list length. Contains returns the subscript of the current array, using the method IndexOf, IndexOf returns 1 if not. The ToArray method returns the form of the list array, using copy in the array directly. In fact, an important method in ArrayList is the iterator, but the iterator is in the parent class abstractlist, and placed in the inner class ITR, when used, directly define an iterator iterator, and then call the iterator method, this method returns the object of the inner class , you can iterate through this object, including the methods Next and Hasnext. This is the most commonly used, here is the key analysis implementation. We can first define an interface type of iterator, using iterator () to get the object directly, this method returns a Itr object (inner class, which exists in the parent class), this inner class contains the NEXT and Hasnext, then you can use this object to iterate through the array. Hasnext Determines whether there is currently an element, which uses the concept of a cursor, whether the current cursor is equal to the size of the current array, and returns True or false. Then use Hasnext to take out the value and move the cursor one bit backwards. This allows you to iterate through the array. Of course this method is invisible in the parent class, in ArrayList. Iterators use a typical interface-oriented programming idea, simply defining the interface type and then invoking the associated method in the implementation class. (Specific analysis will be done later in the programming for the interface). The API says that when an iterator is used, the list cannot be changed, because I did not find it in the JDK. Preliminary analysis is due to the cursor, want to know the netizen to give the answer. (ArrayList is not thread-safe, as the API says, if he is thread-safe, Just put it into the collections.synchronizedlist, this is really seldom used in the work, read the JDK source code, the practice is to put the list into the Synchronizedlist block, the specific implementation does not look, I seldom use it anyway.

LinkedList and ArrayList Many of the functions of the method are the same, but the way of implementation is very different. His underlying use of the list was implemented using the inner class entry,entry containing element, next, and previous. When the list is instantiated, the three variables are assigned, and all the later operations are carried out around the three variables. In this same first analysis method Add,remove,size,contains method. Introduce Addbefore before you introduce Add. Addbefore is to encapsulate the current element into a entry, then next and previous are re-assigned, so that the linked list moves backwards, adding elements to the watch list. Linked list this thing is interested can look at the data structure. The Add method calls the Addbefore, but for next in the list it uses the header, which adds the elements to the list. The cost of the Add method for this list is huge, because he wants to move the list of next and previous, no ArrayList simple, directly assigned to the value. The Remove method is similar in principle to the operation of the linked list. The size method returns the length of the current list and does not introduce too much. The Contains method is the traversal of the linked list, moving the list one step at a way, and matching. The underlying implementation of the ToArray method is to take out the elements of the list, put them in an array, and return the array, and the history wants to return the array directly, as in ArrayList. The LinkedList iterator is similar to the ArrayList iterator, but the underlying implementation is different, such as Hasnext, which uses This.nextindex to determine if there is a next element, rather than looking at whether there are elements in the array. About LinkedList need to look deeply, today I only see some superficial knowledge, will be added later.

Tonight to analyze this, in fact, there are many of these two categories need to see, hope that the vast number of netizens to provide a good analysis, mutual learning.

List analysis of the JDK

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.