ArrayList class is an array that can be dynamically modified. The difference from ordinary arrays is that it has no fixed size limit. We can add or delete elements.
ArrayList inherits AbstractList and implements the List interface.
ArrayList class is located in java.util package. It needs to be introduced before use. The syntax format is as follows:
import java.util.ArrayList; // Introducing the ArrayList class ArrayList<E> objectName =new ArrayList<>(); // initialization
- E: Generic data type, used to set the data type of objectName. It can only be reference data type.
- objectName: object name.
ArrayList is an array queue that provides related functions such as adding, deleting, modifying and traversing.
1 add element
ArrayList class provides many useful methods. You can use the add() method to add elements to ArrayList:
E: Generic data type for setting objectName The data type of can only be reference data type. objectName: Object name. ArrayList Is an array queue, which provides related functions such as adding, deleting, modifying and traversing. 1 Add element ArrayList Class provides many useful methods to add elements to ArrayList have access to add() method:
In the above example, the execution output result is:
[Google, Runoob, Taobao, Weibo]
2 access element
To access the elements in the ArrayList, you can use the get() method:
import java.util.ArrayList; public class RunoobTest { public static void main(String[] args) { ArrayList<String> sites = new ArrayList<String>(); sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); sites.add("Weibo"); System.out.println(sites.get(1)); // Access the second element } }
Note: the index value of the array starts from 0.
In the above example, the execution output result is:
Runoob
3 modify elements
If you want to modify the elements in the ArrayList, you can use the set() method:
import java.util.ArrayList; public class RunoobTest { public static void main(String[] args) { ArrayList<String> sites = new ArrayList<String>(); sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); sites.add("Weibo"); sites.set(2, "Wiki"); // The first parameter is the index position and the second is the value to be modified System.out.println(sites); } }
In the above example, the execution output result is:
[Google, Runoob, Wiki, Weibo]
4 delete element
If you want to delete elements in ArrayList, you can use the remove() method:
import java.util.ArrayList; public class RunoobTest { public static void main(String[] args) { ArrayList<String> sites = new ArrayList<String>(); sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); sites.add("Weibo"); sites.remove(3); // Delete the fourth element System.out.println(sites); } }
In the above example, the execution output result is:
[Google, Runoob, Taobao]
5 calculation size
If you want to calculate the number of elements in the ArrayList, you can use the size() method:
import java.util.ArrayList; public class RunoobTest { public static void main(String[] args) { ArrayList<String> sites = new ArrayList<String>(); sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); sites.add("Weibo"); System.out.println(sites.size()); } }
In the above example, the execution output result is:
4
6 list of iterated algebra groups
We can use for to iterate over the elements in the array list:
import java.util.ArrayList; public class RunoobTest { public static void main(String[] args) { ArrayList<String> sites = new ArrayList<String>(); sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); sites.add("Weibo"); for (int i = 0; i < sites.size(); i++) { System.out.println(sites.get(i)); } } }
In the above example, the execution output result is:
Google Runoob Taobao Weibo
You can also use for each to iterate over elements:
import java.util.ArrayList; public class RunoobTest { public static void main(String[] args) { ArrayList<String> sites = new ArrayList<String>(); sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); sites.add("Weibo"); for (String i : sites) { System.out.println(i); } } }
In the above example, the execution output result is:
Google Runoob Taobao Weibo
7 other reference types
The elements in ArrayList are actually objects. In the above example, the array list elements are of String type.
If we want to store other types, and < E > can only be reference data types, then we need to use the wrapper class of the basic type.
The packing class corresponding to the basic type is as follows:
Basic type | reference type |
---|---|
boolean | Boolean |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
In addition, BigInteger and BigDecimal are used for high-precision operations. BigInteger supports integers with arbitrary precision and is also a reference type, but they have no corresponding basic type.
ArrayList<Integer> li=new Arraylist<>(); // Store integer elements ArrayList<Character> li=new Arraylist<>(); // Store character elements
The following example uses ArrayList to store numbers (using Integer type):
import java.util.ArrayList; public class RunoobTest { public static void main(String[] args) { ArrayList<Integer> myNumbers = new ArrayList<Integer>(); myNumbers.add(10); myNumbers.add(15); myNumbers.add(20); myNumbers.add(25); for (int i : myNumbers) { System.out.println(i); } } }
In the above example, the execution output result is:
10 15 20 25
8 ArrayList sorting
The Collections class is also a very useful class, which is located in the java.util package. The provided sort() method can sort the list of characters or numbers.
The following example sorts letters:
import java.util.ArrayList; import java.util.Collections; // Introduce Collections class public class RunoobTest { public static void main(String[] args) { ArrayList<String> sites = new ArrayList<String>(); sites.add("Taobao"); sites.add("Wiki"); sites.add("Runoob"); sites.add("Weibo"); sites.add("Google"); Collections.sort(sites); // Alphabetic sorting for (String i : sites) { System.out.println(i); } } }
In the above example, the execution output result is:
Google Runoob Taobao Weibo Wiki
The following examples sort numbers:
import java.util.ArrayList; import java.util.Collections; // Introduce Collections class public class RunoobTest { public static void main(String[] args) { ArrayList<Integer> myNumbers = new ArrayList<Integer>(); myNumbers.add(33); myNumbers.add(15); myNumbers.add(20); myNumbers.add(34); myNumbers.add(8); myNumbers.add(12); Collections.sort(myNumbers); // Numerical sorting for (int i : myNumbers) { System.out.println(i); } } }
In the above example, the execution output result is:
8 12 15 20 33 34
Java ArrayList method
The common methods of Java ArrayList are listed as follows:
method | describe |
---|---|
add() | Inserts the element into the arraylist at the specified location |
addAll() | Add all elements in the collection to the arraylist |
clear() | Delete all elements in arraylist |
clone() | Copy an arraylist |
contains() | Determine whether the element is in arraylist |
get() | Get elements in arraylist by index value |
indexOf() | Returns the index value of the element in arraylist |
removeAll() | Deletes all elements that exist in the arraylist in the specified collection |
remove() | Delete individual elements in arraylist |
size() | Returns the number of elements in arraylist |
isEmpty() | Determine whether arraylist is empty |
subList() | Intercept some arraylist elements |
set() | Replace the element of the specified index in arraylist |
sort() | Sort arraylist elements |
toArray() | Convert arraylist to array |
toString() | Convert arraylist to string |
ensureCapacity() | Sets the arraylist of the specified capacity size |
lastIndexOf() | Returns the last occurrence of the specified element in the arraylist |
retainAll() | Keep those elements in arraylist that also exist in the specified collection |
removeIf() | Delete all arraylist elements that meet specific conditions |
forEach() | Traverse each element in the arraylist and perform specific operations |
containsAll() | Checks whether the arraylist contains all the elements in the specified collection |
trimToSize() | Adjust the capacity in arraylist to the number of elements in the array |
removeRange() | Deletes the elements that exist between the specified indexes in the arraylist |
replaceAll() | Replace each element in the array with the given operation content |
removeIf() | Delete all arraylist elements that meet specific conditions |
forEach() | Traverse each element in the arraylist and perform specific operations |