Java ArrayList clear

This section demonstrates how to use the clear method in Java ArrayList

 
 

Share on Google+Share on Google+

Java ArrayList clear

Ads

This section demonstrates how to use the clear method in Java ArrayList

  • It removes all the elements of the given list and returns empty list
  • But the remove method removes element one by one


Java Arraylist Clear Example
import java.util.*;

public class List1 {

public static void main(String[] args) {
String  ar[]={"india","pakistan","United Kingdom","Japan"};
List list=new ArrayList();
list.add(ar[0]);
list.add(ar[1]);
list.add(ar[2]);
list.add(ar[3]);

System.out.println(list);
list.clear();
System.out.println(list);

}
}
Output
[india, pakistan, United Kingdom, Japan]
[]

Advertisements

Posted on: October 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles