import java.util.ArrayList; import java.util.List; public class ListClearExample { public static void main(String[] args) { List list = new ArrayList(); list.add("apple"); list.add("banana"); list.add("cherry"); list.clear(); System.out.println("List after clear(): " + list); // List after clear(): [] } }
import java.util.ArrayList; import java.util.List; public class ListClearExample { public static void main(String[] args) { ListIn this example, we have created a generic ArrayList and added three elements to it. After that, we called the clear method to remove all the elements from the list. The output will be an empty list. Package library: java.util.list = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("cherry"); list.clear(); System.out.println("List after clear(): " + list); // List after clear(): [] } }