Java Collections Java Java API
java.util.ArrayList
public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.
package com.logicbig.example.arraylist;import java.util.*;public class ClearExample { public static void main(String... args) { // Remove all elements ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C")); System.out.println("Before clear - Size: " + list.size()); list.clear(); System.out.println("After clear - Size: " + list.size()); System.out.println("Is empty? " + list.isEmpty()); }}
Before clear - Size: 3After clear - Size: 0Is empty? true