Java Array and ArrayList

  • 2020-05-27 05:41:09
  • OfStack

Java Array differs from ArrayList

1) incisive elaboration:

Think of ArrayList as an "Array that automatically expands its capacity."

2) Array ([]) : most efficient; But its capacity is fixed and cannot be changed dynamically.

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

3) Suggestions:

For efficiency and type validation, Array should be used whenever possible, and ArrayList should be used when the array size cannot be determined!

But when you're trying to solve a more general problem, Array can be too limited.

4) all objects are 1 in Java, and Array is also an object. No matter what type of Array you use,

The Array name itself is actually an reference, pointing to an actual object within heap.

This object can be generated automatically via the Array initialization syntax or manually as an new expression.

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

6) the object array is almost identical to the basic type array in application, except that the former holds reference and the latter directly holds the values of the basic type.
Such as:

string [] staff=new string[100];
int [] num=new int[10];

7) the container actually holds 1 reference pointing to Object so that it can store any type. This does not, of course, include the base type, which does not inherit from any classes.

8) in the face of Array, we can directly hold Array (e.g., int [] num;). , can also hold reference (pointing to objects) Array; However, the container class can only hold reference (pointing to objects), and to place the base type inside the container, you need to use the wrapper class. However, the wrapper class may not be easy to get started with, and in addition, the efficiency of primitives Array is much better than that of a container that "holds classes other than the base type (reference)."

Of course, if you're working with a basic type of object and need to automatically expand capacity when you run out of space, Array won't work, so you'll have to use a container with an enclosing class.

9) in some cases, container classes can still function correctly even if they have not been transformed to the original type. One situation stands out: the compiler provides some extra support for String class to make it work smoothly.

10) some basic operations on arrays, such as sorting, searching and comparing, are common. Thus, Arrays class assistance is provided in Java for these operations: sort(),binarySearch(),equals(),fill(),asList().

However, the Arrays class does not provide a delete method, while the remove() method is available in ArrayList. I wonder if it is the reason why you do not need to do such operations in Array (because the linked list should be used at this time).

11) the use of ArrayList is also simple: generate ArrayList, use add() to put objects in, and use get(i) to pull them out with index values. This cut is exactly the same as Array, only less [].

2. References:

1) efficiency:

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

Whenever Add, AddRange, Insert, InsertRange add elements such as method, will check whether the internal array capacity is not enough, if it is, it will double the current capacity to rebuild one array, and the old element Copy into the new array, then discarding the old array, in this critical point of expansion, is affecting the efficiency of the comparison should be.

ArrayList is a complex version of Array

ArrayList internally encapsulates an array of type Object. In the sense of 1, it is not substantially different from an array, and even many methods of ArrayList, such as Index, IndexOf, Contains, Sort, etc., call the corresponding methods of Array directly on the basis of the internal array.

2) type identification:

When ArrayList is stored in an object, type information is discarded and all objects are shielded as Object. No type checks are made at compile time, but errors are reported at run time.
The main difference between ArrayList and arrays is the efficiency of dynamic capacity increase

3) ArrayList can store any Object, such as String, etc.

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

Related articles: