ArrayList is a part of the collection framework. It is present in the java.util package and provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. We can dynamically add and remove items. It automatically resizes itself. The following is an example to demonstrate the implementation of the ArrayList
Here is the sample code
Copy to Clipboard
x
1
import java.util.ArrayList;
2
import java.util.List;
3
4
public class ArrayList_1 {
5
public static void main(String[] args) {
6
7
// Declaring an ArrayList
8
List<Integer> testArravyList = new ArrayList<Integer>();
9
//Appending new elements at the end of the list
10
for (int i = 1; i <= 5; i++) {
11
testArravyList.add(i);
12
}
13
//printing arraylist element;
14
System.out.println(testArravyList);
15
16
//Removing an element from list
17
testArravyList.remove(3);
18
19
//priting the arraylist after removed element
20
System.out.println(testArravyList);
21
}
22
}
23
Copy to Clipboard
6
1
Sample Output
2
[1, 2, 3, 4, 5]
3
[1, 2, 3, 5]
4
5
Process finished with exit code 0
6
Leave A Comment