Java ArrayList clear() Method



Description

The Java ArrayList clear() method removes all of the elements from this list.The list will be empty after this call returns.

Declaration

Following is the declaration for java.util.ArrayList.clear() method

public void clear()

Parameters

NA

Return Value

This method does not return any value.

Exception

NA

Clearing an ArrayList of Integers Example

The following example shows the usage of Java ArrayList clear() method. In this example, we're using Integers. As first step, we're populating the arrayList object and printing it. Then we're print the size of the arrayList object and perform clear operation. After clearing, we're printing the size of the arrayList object which is 0 now.

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
     
      // create an empty array list
      ArrayList<Integer> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(20);
      arrayList.add(30);
      arrayList.add(10);

      // let us print all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);    

      // finding size of this arrayList
      int retval = arrayList.size();
      System.out.println("ArrayList consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      arrayList.clear();
      retval = arrayList.size();
      System.out.println("Now, ArrayList consists of "+ retval +" elements");
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = [20, 30, 10]
ArrayList consists of 3 elements
Performing clear operation !!
Now, ArrayList consists of 0 elements

Clearing an ArrayList of Strings Example

The following example shows the usage of Java ArrayList clear() method. In this example, we're using Strings. As first step, we're populating the arrayList object and printing it. Then we're print the size of the arrayList object and perform clear operation. After clearing, we're printing the size of the arrayList object which is 0 now.

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
     
      // create an empty array list
      ArrayList<String> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");

      // let us print all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);    

      // finding size of this arrayList
      int retval = arrayList.size();
      System.out.println("ArrayList consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      arrayList.clear();
      retval = arrayList.size();
      System.out.println("Now, ArrayList consists of "+ retval +" elements");
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = [A, B, C]
ArrayList consists of 3 elements
Performing clear operation !!
Now, ArrayList consists of 0 elements

Clearing an ArrayList of Objects Example

The following example shows the usage of Java ArrayList clear() method. In this example, we're using Student objects. As first step, we're populating the arrayList object and printing it. Then we're print the size of the arrayList object and perform clear operation. After clearing, we're printing the size of the arrayList object which is 0 now.

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
     
      // create an empty arrayList
      ArrayList<Student> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(new Student(1, "Julie"));
      arrayList.add(new Student(2, "Robert"));
      arrayList.add(new Student(3, "Adam"));

      // let us print all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);    

      // finding size of this arrayList
      int retval = arrayList.size();
      System.out.println("arrayList consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      arrayList.clear();
      retval = arrayList.size();
      System.out.println("Now, arrayList consists of "+ retval +" elements");
   } 		
} 

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}  

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
arrayList consists of 3 elements
Performing clear operation !!
Now, arrayList consists of 0 elements
java_util_arraylist.htm
Advertisements