The difference between Java Array and ArrayList
1) Incisive explanation:
Think of ArrayList as an "Array that automatically amplifies capacity".
2) Array([]): the most efficient; but its capacity is fixed and cannot be changed dynamically;
ArrayList: Capacity can grow dynamically; but sacrifice efficiency;
3) Suggestions:
Based on efficiency and type verification, Array should be used as much as possible. ArrayList should be used only if the array size cannot be determined!
But when you try to solve more general problems, Array's functionality 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 a real object within the heap.
This object can be generated automatically via the "Array initialization syntax" or manually in a new expression.
5) Array can be used as a function return value because it itself is the reference of the object;
6) The object array is almost exactly the same as the basic type array in use. The only difference is that the former holds a reference, while the latter directly holds the value of the basic type;
For example:
string [] staff=new string[100]; int [] num=new int[10];
7) What the container holds is actually a reference pointing to the Object, so that any type can be stored. Of course this does not include the basic type, because the basic type does not inherit from any classes.
8) In the face of Array, we can directly hold the Array of the basic type value (for example: int [] num;), or the Array of the reference (pointing to the object); but the container class can only hold the reference (pointing to the object). To place the basic type in the container, we 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 the container that "accepts references of basic types".
Of course, if your operating object is of the basic type and needs to automatically amplify the capacity when there is insufficient space, Array will not be suitable, so you have to use a cover type container.
9) In some cases, container classes can still operate correctly even if they have not transformed to their original type. There is a situation that is especially special: the compiler provides some extra support for the String class to make it work smoothly.
10) Some basic operations on arrays, such as sorting, searching and comparison, are very common. Therefore, the Arrays class is provided in Java to assist in these operations: sort(), binarySearch(), equals(), fill(), asList().
However, the Arrays class does not provide a delete method, and there is a remove() method in ArrayList. I don’t know if it is because it is not necessary to do delete operations in Array (because you should use the linked list at this time).
11) The use of ArrayList is also very simple: generate an ArrayList, use add() to place objects, and use get(i) to match the index value to remove them. All of this is exactly the same as Array's use, but there is only less [].
2. Reference materials:
1) Efficiency:
Array expansion is a factor that has a greater impact on ArrayList efficiency.
Whenever methods such as Add, AddRange, Insert, InsertRange are executed, they will check whether the capacity of the internal array is insufficient. If so, it will rebuild an array at twice the current capacity, copy the old elements into the new array, and then discard the old array. The expansion operation at this critical point should be more influential in efficiency.
ArrayList is a complex version of Array
ArrayList encapsulates an Object-type array. In general, it has no essential difference from an array. Even many methods of ArrayList, such as Index, IndexOf, Contains, Sort, etc., directly call Array's corresponding methods based on the internal array.
2) Type identification:
When ArrayList stores an object, the type information is discarded and all objects are blocked as Object. The type is not checked during compilation, but an error will be reported at runtime.
The difference between ArrayList and array is mainly due to the efficiency of dynamic capacity increase.
3) ArrayList can store any Object, such as String, etc.
Thank you for reading, I hope it can help you. Thank you for your support for this site!