3:35 am - Saturday August 19, 2017

Clear elements example program to clear the elements in the arraylist using clear() method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Clear elements example program to clear the elements in the arraylist using clear() method

Example Program


import java.util.ArrayList;

public class ClearArraylist {

	public static void main(String[] args) {

		ArrayList<Integer> arrlist = new ArrayList<Integer>(5);

		// use add() method to add elements in the list

		arrlist.add(20);

		arrlist.add(30);

		arrlist.add(10);

		arrlist.add(50);

		// to print all the elements available in list

		for (Integer n : arrlist) {

			System.out.println("Number = " + n);

		}

		// finding size of this list

		int returnvalue = arrlist.size();

		System.out.println("List consists of " + returnvalue + " elements");

		System.out.println("Performing clear operation !!");

		arrlist.clear();

		returnvalue = arrlist.size();

		System.out
				.println("Now, list consists of " + returnvalue + " elements");

	}

}

 

Output

 

Number = 20

Number = 30

Number = 10

Number = 50

List consists of 4 elements

Performing clear operation !!

Now, list consists of 0 elements

Description

The java.util.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

 

BACK

Filed in: Z-A All Java Codes

Comments are closed.