CodesJava

Easy learning with example program codes

Remove all elements from arraylist in java

The clear() method is used to remove all elements from arraylist in java.

Syntax:

arrayList.clear();

Example:

package com.codesjava;
 
import java.util.ArrayList;
 
public class Test {
  public static void main(String args[]){
	ArrayList<String> arrayList = new ArrayList<String>();
	arrayList.add("Jai");
	arrayList.add("Mahesh");
	arrayList.add("Vivek");
	arrayList.add("Naren");
	arrayList.add("Hemant");
	arrayList.add("Vishal");
	System.out.println("Actual ArrayList:"+arrayList);
	arrayList.clear();
        System.out.println("ArrayList after clear :"+arrayList);
  }
}

Output

Actual ArrayList:[Jai, Mahesh, Vivek, Naren, Hemant, Vishal]
ArrayList after clear :[]
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status