Saturday, October 20, 2012

clear method Example in java

import java.util.*;
public class clearMethod {

    public static void main(String[] args) {
       
        ArrayList a1 = new ArrayList();
        a1.add(1);
        a1.add(2);
        a1.add(3);
        a1.add(4);
        a1.add(5);
       
        System.out.println("ArrayList size is " + a1.size());
        /*
          After the addition of 5 numbers to the ArrayList the size becomes 5
        */
        a1.clear();
        /*
          now after the clear method is invoked in the ArrayList its size becomes zero
        */
        System.out.println("ArrayList size is now " + a1.size());

    }

}


Answer : 
------------
ArrayList size is 5
ArrayList size is now 0

No comments:

Post a Comment