Detailed explanation of the difference between Java array and ArrayList

The difference between Java array and ArrayList

1) Incisive exposition:

You can think of ArrayList as an "array that automatically expands capacity".

2) Array ([]): the most efficient; however, its capacity is fixed and cannot be changed dynamically;

ArrayList: capacity can grow dynamically; But at the expense of efficiency;

3) Recommendations:

Based on efficiency and type checking, array should be used as much as possible. ArrayList should be used only when the array size cannot be determined!

However, when you try to solve more general problems, the function of array may be too limited.

4) Everything in Java is an object, and array is also an object. Regardless of the array type you use,

The array name itself is actually a reference, pointing to an actual object within the heap.

This object can be generated automatically via the "array initialization syntax" or manually with a new expression.

5) Array can be used as the return value of the function because it is the reference of the object;

6) as like as two peas, the array of objects is almost the same as the basic type array. The only difference is that the former holds reference and the latter holds the value of the basic type directly. For example:

7) The container actually holds references pointing to objects, so that any type can be stored. Of course, this does not include primitives, because primitives do not inherit from any classes.

8) Facing the array, we can directly hold the array of basic type values (for example: int [] num;), You can also hold an array of references (pointing to objects); however, container classes can only hold references (pointing to objects). If you want to place a primitive in a container, you need to use the wrapper class. However, the wrapper class may not be easy to use. In addition, the efficiency of primitives array is much better than that of a container that "holds an override of a primitive".

Of course, if your operation object is a basic type and you need to automatically expand the capacity when there is insufficient space, array is not suitable. At this time, you have to use the container of the outer class.

9) In some cases, the container class can still work without transformation to the original type. One particular case is that the compiler provides some additional support for string class to make it work smoothly.

10) Some basic operations on arrays, such as sorting, search and comparison, are very common. Therefore, the arrays class is provided in Java to assist these operations: sort(), binarysearch(), equals(), fill(), aslist()

However, the arrays class does not provide a delete method, and there is a remove () method in the ArrayList. I don't know whether it's the reason why you don't need to delete in the array (because you should use the linked list at this time).

11) The use of ArrayList is also very simple: generate ArrayList, use add() to put objects in, and use get (I) to take them out with the index value. All this is exactly the same as the use of array, except [].

2. References:

1) Efficiency:

Array expansion is a factor that has a great impact on the efficiency of ArrayList.

Whenever the methods of adding elements such as add, addrange, insert and insertrange are executed, it will check whether the capacity of the internal array is insufficient. If so, it will rebuild an array with twice the current capacity, copy the old elements into the new array, and then discard the old groups. The capacity expansion operation at this critical point should affect the efficiency.

ArrayList is a complex version of array

ArrayList internally encapsulates an array of object type. In a general sense, it is not fundamentally different from array. Even many methods of ArrayList, such as index, indexof, contains, sort, etc., directly call the corresponding method of array on the basis of internal array.

2) Type identification:

When an ArrayList is stored in an object, the type information is discarded, and all objects are masked as objects. The type is not checked during compilation, but an error will be reported at run time. The difference between ArrayList and array is mainly due to the efficiency of dynamic resizing

3) ArrayList can store any object, such as string.

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>