Explanation of Java ArrayList implementation example

ArrayList overview:

ArrayList is implemented based on array. It is a dynamic array. Its capacity can grow automatically, which is similar to the dynamic memory application and dynamic memory growth in C language.

ArrayList is not thread safe and can only be used in a single threaded environment. Collections can be considered in a multi-threaded environment The synchronizedlist (List L) function returns a thread safe ArrayList class. You can also use the copyonwritearraylist class under concurrent and contract.

ArrayList implements the serializable interface, so it supports serialization, can transmit through serialization, implements the randomaccess interface, and supports fast random access. In fact, it implements the clonable interface through subscript serial number, which can be cloned.

Each ArrayList instance has a capacity, which is the size of the array used to store list elements. It is always at least equal to the size of the list. As you add elements to the ArrayList, its capacity grows automatically. Automatic growth will cause data to be re copied to the new array. Therefore, if you can predict the amount of data, you can specify its capacity when constructing ArrayList. Before adding a large number of elements, the application can also use the ensurecapacity operation to increase the capacity of ArrayList instances, which can reduce the number of incremental redistribution.

Note that this implementation is not synchronous. If multiple threads access an ArrayList instance at the same time, and at least one thread modifies the list structurally, it must maintain external synchronization.

Let's make a record and summary of Java ArrayList

A generic class, an object array and a private variable are defined to record the number of elements in the collection. The original text has an additional private variable. I don't know why to use it. The author didn't explain or mention it. It's ok if I didn't use it

Three initialization methods, initialize the array according to the default size, initialize the given size and pass a class that inherits the collection collection interface for conversion and assignment initialization

This is a core method. The expansion of the set is actually the expansion of the array and the size of the mincapacity set. It is used to compare and judge whether the expansion should be carried out. Arrays The copyof() method is used for capacity expansion,

This method copies the contents of the first parameter into a new array. The size of the array is the second parameter and returns a new array. Detailed comments on the olddata variable are given above

Whether 1 is retrieved for a subscript/**

To add an element, expand the capacity first, assign a value to the element, and then add one to the element. Note that the size + 1 field does not add one. Here is an arithmetic operation, so self increment is required later

The difference here is system arraycopy(elementData,size-index);

This is an internal method of C, which is explained in detail in the original text. It will not be mentioned here. This is also the core of the whole ArrayList, also known as arrays The internal implementation principle of copyof()

Basically, other methods only carry out different processing according to different situations. For example, pass in the data object through the interface, obtain the length for capacity expansion, and copy the data into a new array using system and array copy

Basically, they operate on arrays and use the method of C for assignment and movement. You can check the original text in detail. There are not many problems in the original text except for the private variable. The code can run perfectly. What Li should pay attention to and difficulties are system, arraycopy and arrayist The use of the two methods of copy () and the variable of olddata in the expansion method is really good. I don't know why to use this variable at the beginning. It will be explained at the end of the original text.

The above is the explanation of the implementation example of Java ArrayList introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message. Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>