Java Arraylist Clear() Function

clear function in python

What is an ArrayList?

Here, in the page we will discuss about the ArrayList’s clear() Method in java.
Arraylist is a part of collection framework in java. It contains some inbuilt function like clear() function which can be found in the java.util.ArrayList Package.It implements the List interface of the collections framework.

ArrayList clear Function:

Arraylist contains several inbuilt functions that are used to perform several operations on arraylist in Java. clear() function is also an inbuilt method of class ArrayList.
clear() method is used to clear a whole object of an arraylist.

Syntax:

arraylist_name.clear()

Definition of Parameters:

arrayList_name : This contains the name of the arraylist in which the functions have to be performed.
clear method in java

Example for Clearing ArrayList of String Type:

Run
import java.util.*;

public class Main{
  public static void main(String[] args){
    // Initializing an ArrayList
    ArrayList PrepInsta = new ArrayList<>();

    // Adding elements to the ArrayList
    PrepInsta.add("Lakshit");
    PrepInsta.add("Rishikesh");
    PrepInsta.add("Rishik");
    
    // Printing the ArrayList
    System.out.println("ArrayList: " + PrepInsta);
    
    // invoking the clear function
    PrepInsta.clear();
    System.out.println("ArrayList: " + PrepInsta);
  }
}

Output:

ArrayList: [Lakshit, Rishikesh, Rishik]
ArrayList: []

Explanation:

In the above Program, we have created an arraylist PrepInsta containing its employees name and after invoking the clear() method the arraylist “PrepInsta” will remove all its elements and gets empty.

Example for Clearing ArrayList of Integer Type:

We can also clear the ArrayList of Integer type with the help of clear() function.

Run
import java.util.*;

public class Main{
  public static void main(String[] args){
    // Initializing the new ArrayList
    ArrayList Numbers = new ArrayList<>();

    // Adding elements to the Arraylist
    Numbers.add(1);
    Numbers.add(2);
    Numbers.add(3);

    // Printing the ArrayList
    System.out.println("ArrayList: " + Numbers);
    
    // Invoking the clear function
    Numbers.clear();
    System.out.println("ArrayList: " + Numbers);
  }
}

Output:

ArrayList: [1, 2, 3]
ArrayList: []

Explanation:

In the above Example. we add 3 elements of integer type to an arraylist name “Numbers” and after invoking the clear() method the arraylist “PrepInsta” will remove all its elements and gets empty.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription