May 28, 2020
Hot Topics:

Objects and Collections: ArrayLists

  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Although you will continue your exploration of the ArrayList, and Lists in general, in the next article, the last topic you will look at this month is how to remove items from the ArrayList. Again, you can specify a location for removal of a particular item. This is accomplished with the remove() method.


It is important to note that, when the element is removed, the subsequent elements move up one location in the list. This is to say that the internal pointers of the ArrayList are rebuilt so that the removed element is eliminated from the list.

Listing 8 contains code that will add items to myList, as before, and then remove the element at location 1.

Listing 8: TestArrayList.java

import java.util.ArrayList;
import java.util.Iterator;

class TestArrayList{

   public static void main(String[] args){

      int pos = 0;

      ArrayList<String> myList = new ArrayList<String>();


      System.out.println("\nInitial List");

      pos = 0;
      for (String element : myList) {
         System.out.println(pos + ":" + element);


      System.out.println("\nRemove from List");

      pos = 0;
      for (String element: myList) {
         System.out.println(pos + ":" + element);

When you execute this program, you can see that the element in location 1 of myList has been successfully removed and all the other elements moved up a single location.

C:\column26>"C:\Program Files\Java\jdk1.5.0_07\bin\java"
   -classpath . TestArrayList

Initial List

Remove from List
1:Two        // item a location 1 removed


In this article, the syntax of an ArrayList was introduced and you transitioned from the Vector to the ArrayList. You also introduced the concept of generics and how they impact code written for the Java SE 1.5 as well as code written before the Java SE 1.5 release.

Next month, you will explore in much more detail that topic of generics and the parent classes of the ArrayList. When you look at the API for the class ArrayList, as presented below, you can see that there are many other classes that play a part in the implementation of an ArrayList, and Lists in general.

java.util: Class ArrayList<E>


All Implemented Interfaces:

Direct Known Subclasses:


  • www.sun.com
  • Just Java 2, 6th Edition. Peter van der Linden. 2004, Sun Microsystems.

About the Author

Matt Weisfeld is a faculty member at Cuyahoga Community College (Tri-C) in Cleveland, Ohio. Matt is a member of the Information Technology department, teaching programming languages such as C++, Java, and C# .NET as well as various web technologies. Prior to joining Tri-C, Matt spent 20 years in the information technology industry gaining experience in software development, project management, business development, corporate training, and part-time teaching. Besides the first edition of The Object-Oriented Thought Process, Matt has published two other computer books, and more than a dozen articles in magazines and journals such as Dr. Dobb's Journal, The C/C++ Users Journal, Software Development Magazine, Java Report, and the international journal Project Management.

Page 3 of 3

This article was originally published on September 11, 2006

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date