EN
Java - clear ArrayList
0 points
In this article, we would like to show you how to clear the ArrayList in Java.
Quick solution:
xxxxxxxxxx
1
myArrayList.clear();
In this example, we use clear()
method to remove all items from the fruits
ArrayList.
xxxxxxxxxx
1
import java.util.ArrayList;
2
3
public class Example {
4
5
public static void main(String[] args) {
6
List<String> fruits = new ArrayList<>();
7
8
// add items to the ArrayList
9
fruits.add("Apple");
10
fruits.add("Banana");
11
fruits.add("Cherry");
12
13
// remove all items
14
fruits.clear();
15
16
System.out.println(fruits); // []
17
}
18
}
Output:
xxxxxxxxxx
1
[]