The difference between array Arrays ArrayList in Java __java

Source: Internet
Author: User

Sometimes in the Java language, the array, arrays and ArrayList get the fog, what is the connection between them? Look at the relevant source code, finally found some clues. Make a note for easy viewing.

1. Array

The relationship between array and [] is like int and Integer, Boolean, and Boolean. Its constructors are private, and the assignment provides static values such as Setint (object arr, int index, int i) setfloat (object arr, int index, float f) Set (object arr, int i Ndex, Object value), which provides methods such as static GetInt (object arr, int index) Get (object arr, int index). In JDK1.6 's source code, these methods are implemented locally. Because the local code is not found, not in-depth learning, to view the source of the android4.2, these methods are not implemented in local code, directly based on the actual type, the allocation of the array.

2. Arrays

Arrays mainly provides ToString (), sorting, binary search, copyof () and other methods. The same construction method is private, toString () sort lookup and copyof () and other methods are also static, which for the basic types of random data used in the dual-hub fast sorting algorithm (Dualpivotquicksort), for disorderly object[] The merge sort is used (however, note that the merge sort is a legacy issue that will be deleted in future releases). Next, I want to make a note of the commonly used sorting algorithms. Copyof () assigns a temporary array and eventually calls System.arraycopy () to complete the copy of the array element.

3. ArrayList

ArrayList this peacetime use of more, feel more familiar with the spirit of curiosity, looked at the realization. The interior of the ArrayList is stored with object[], starting with the default of 10, and when you call add and other methods, the array is checked for full, and if filled, the elements in the array are called by the arrays.copyof () by half the current length of the array. When the Remove () method is invoked, System.arraycopy () is eventually invoked to move the elements in the array.

The ArrayList method of adding and removing both ends directly or indirectly calls the System.arraycopy () method. That arraycopy () exactly what kind of Zhang. In the System.java file, you see the prototype of the Arraycopy () method, which is also a native method. Search the JDK source code and Android4.2 source of the local codes, have not found its implementation. (Wondering where these local methods are actually implemented.) Virtual machine in the dynamic library?)

Attempt to find the shadow of Arraycopy () from all. So files in the JRE directory. The trace of Arraycopy was finally found from the export symbol of the libjvm.so file. Sure enough, in the libjvm.so file. The Getint (), Getboolean (), and so on are also found in the exported symbols of the libjava.so file.


Note:

Reference to the JDK source version is jdk1.7.0_25, Android ' source is 4.2.

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.