ArrayList remove Method Tutorial

There are two overloaded versions of the .remove() method. Both of them simply remove a record from an instance of an ArrayList object. There are some things to look out for when it comes to ArrayList objects that contain Integer wrapper class objects.

  • E remove(int index) - removes the element at the index and returns an instance of the element that was removed.
  • boolean remove(Object o) - removes the first element in the ArrrayList that matches the object argument, returns true if removed.



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 ArrayListRemove
C:\Java>cd ArrayListRemove
C:\Java\ArrayListRemove>Notepad ArrayListRemove.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 ArrayListRemove {
    public static void main(String args[]) {
        Object returnVal;
        ArrayList<String> states = new ArrayList<String>();
        Collections.addAll(states, "Alaska", "Alabama", "California", "Colorado", "Hawaii", "New York");
        System.out.println("states = " + states);
    
        returnVal = states.remove(3);
        System.out.println("states.remove(3), states = " + states);
        System.out.println("returnVal = " + returnVal);

        returnVal = states.remove("California");
        System.out.println("states.remove(\"California\"), states = " + states);
        System.out.println("returnVal = " + returnVal);
        System.out.println("\n----------\n");

        ArrayList<Integer> integers = new ArrayList<Integer>();
        Collections.addAll(integers, 1001, 1002, new Integer(1003), new Integer(1004), 1005, 1006, 1007);
        System.out.println("integers = " + integers);
    
        returnVal = integers.remove(3); // what happens here ???
        System.out.println("integers.remove(3), integers = " + integers);
        System.out.println("returnVal = " + returnVal);

        //returnVal = integers.remove(1005); // what happens here ???
        //returnVal = integers.remove((Integer) 1005); // what happens here ???
        //System.out.println("integers.remove((Integer) 1005), integers = " + integers);
        //System.out.println("returnVal = " + returnVal);
    }
}

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


C:\Java\ArrayListRemove>javac ArrayListRemove.java
C:\Java\ArrayListRemove>java ArrayListRemove
states = [Alaska, Alabama, California, Colorado, Hawaii, New York]
states.remove(3), states = [Alaska, Alabama, California, Hawaii, New York]
returnVal = Colorado
states.remove(\"California\"), states= [Alaska, Alabama, Hawaii, New York]

--------------

integers = [1001, 1002, 1003, 1004, 1005, 1006, 1007]
integers.remove(3), integers = [1001, 1002, 1003, 1005, 1006, 1007]
returnVal = 1004


Final thoughts

None


Tutorials