Home >Java >Javagetting Started >The difference between array (Array) and list (ArrayList) in java
List (ArrayList) is an enhancement to array (Array). The ways to allocate array lists and create arrays are as follows:
Assign array lists:
new ArrayList<Employee>(100);
Creating an array:
new Employee[100];
Online video tutorial recommendation: java course
The difference between the two:
1: Space size
1. The space size of Array is fixed, and you cannot apply again when the space is insufficient, so you need to determine the appropriate space size in advance.
2. The space of ArrayList grows dynamically. If there is not enough space, it will create a new array with a space 0.5 times larger than the original space, then copy all elements to the new array, and then discard the old array. Moreover, every time a new element is added, it is checked whether there is enough space in the internal array.
2: Storage content
1. Array array can contain basic types and object types.
2. ArrayList can only contain object types.
It should be noted that Array arrays must contain elements of the same type when stored. This is not necessarily true for ArrayList, because ArrayList can store Object.
Three: Method
As an enhanced version of Array, ArrayList is of course more diverse than Array in terms of methods. For example, add all addAll()
, delete all removeAll()
, return the iterator iterator()
, etc.
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 If we want to save data in the form of an array without adding operations to the data, but just to facilitate our search, then we can choose ArrayList.
And there is another thing that we must know, that is, if we need to move or delete elements frequently, or deal with an extremely large amount of data, then using ArrayList is really not a good choice. , because its efficiency is very low, and it is very troublesome to use arrays to perform such actions. Then, we can consider choosing LinkedList.
Recommended related articles and tutorials: Introduction to java language
The above is the detailed content of The difference between array (Array) and list (ArrayList) in java. For more information, please follow other related articles on the PHP Chinese website!