How to Remove All ArrayList Elements in Java


Here we may learn how to remove all elements from ArrayList object with clear method in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;

public class RemoveAllElementsArrayListExample {

	public static void main(String[] args) {

		ArrayList al = new ArrayList();

		al.add("A");
		al.add("B");
		al.add("C");
		al.add("D");

		//The collection to be removed needs to be specified for removal
		al.removeAll(al);

		System.out.println("At this time, the number of elements:" + al.size());

	}

}

Output:

At this time, the number of elements:0

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments