ArrayList clear() and clone() Tutorial

void clear()
Object clone()

In this tutorial I will demonstrate how to use two methods in the ArrayList class. Both the .clear() and .clone() methods have no parameters in their parameter list. The .clear() method is self-documenting and it does just what you might expect ... it clears all the elements from the current instance of the ArrayList. The .clone() method may not work quite as expected - it does create a seperate copy of the ArrayList object with the same values as the original. However, it returns an Object type, and that means that none of the ArrayList methods can be called except for those inherited from the Object class. I'll explain in the source code.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ArrayListClearClone
C:\Java>cd ArrayListClearClone
C:\Java\ArrayListClearClone>Notepad ArrayListClearClone.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.util.*;

class ArrayListClearClone {
    public static void main(String args[]) {        
        ArrayList<String> strList = new ArrayList<String>();
        Collections.addAll(strList, "Alaska", "Alabama", "California" );
        strList.add("Colorado");
        strList.addAll(Arrays.asList("Hawaii", "New York"));
        System.out.println("strList = "  + strList);

        Object cloneList = strList.clone();
        System.out.println("cloneList = " + cloneList);
        System.out.println("cloneList instance class = " + cloneList.getClass());
        System.out.println();
    
        strList.clear();
        System.out.println("after strList.clear() strList = " + strList);
        System.out.println("cloneList is unaffected: " + cloneList);
        System.out.println();
        // cloneList reference variable is of type Object, not ArrayList. 
        // We cannot call ArrayList methods that are not inherited from Object!
        // cloneList.clear(); // error - clear() method does not exist in Object!
           
        //ArrayList<String> newList = cloneList; // error
        //ArrayList<String> newList = (ArrayList<String>)cloneList;
        //System.out.println("newList = " + newList); 
        //newList.clear();
        //System.out.println("after newList.clear() newList = " + newList);
        //System.out.println("cloneList = " + cloneList);
    }
}

Now switch back to the command prompt (CMD) and type in javac ArrayListClearClone.java and press Enter.
Now type in java ArrayListClearClone and press Enter.


C:\Java\ArrayListClearClone>javac ArrayListClearClone.java
C:\Java\ArrayListClearClone>java ArrayListClearClone
strList = [Alaska, Alabama, California, Colorado, Hawaii, New York]
cloneList = [Alaska, Alabama, California, Colorado, Hawaii, New York]
cloneList instance class = class java.util.ArrayList

after strList.clear() strList = []
cloneList is unaffected: [Alaska, Alabama, California, Colorado, Hawaii, New York]

newList= [Alaska, Alabama, California, Colorado, Hawaii, New York]
after newList.clear() newList = []
closeList = []
    


Final thoughts

It is a good practice to always check and understand the documentation when invoking methods. Personally, I wanted the .clone() method to return an ArrayList type since we are in the ArrayList class, but that is not the case.


Tutorials