What is the difference between an Array (Array) and a List (ArrayList)? When should I use Array instead of ArrayList?

1. Comparison of storage content:
Array array can contain basic types and object types,
but ArrayList can only contain object types.
Array arrays must have elements of the same type when stored. ArrayList is not necessarily.

2. Comparison of space size:
The space size of Array array is fixed, so it is necessary to determine the appropriate space size in advance.
The space of the ArrayList is dynamically increased, and each time a new element is added, it is checked whether the space in the internal array is sufficient.

3. Comparison of methods:
ArrayList is more diverse in method than Array, such as adding all addAll(), deleting all removeAll(), returning iterator() and so on.

Applicable scenarios:
If we want to save some data that will exist and remain unchanged during the entire program running, we can put them into a global array, but if we simply want to save the data in the form of an array, without performing any processing on the data If the operations such as adding are only convenient for us to search, then we choose ArrayList.
If we need to move or delete elements frequently, or deal with a large amount of data, then using ArrayList is really not a good choice, because its efficiency is very low, using arrays for such actions is very Trouble, then, we can consider choosing LinkedList.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325943466&siteId=291194637