Objects and Collections: ArrayLists

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.

myList.remove(1);

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.

Conclusion

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.

Direct Known Subclasses:

References

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.