When we work with arrays in Java, we’ve been limited by the fact that once an array is created, it has a fixed size. We can’t add or remove elements.
But what if we needed to add to the book lists, newsfeeds, and other structures we were using arrays to represent?
To create mutable and dynamic lists, we can use Java’s
ArrayLists allow us to:
- Store object references as elements
- Store elements of the same type (just like arrays)
- Access elements by index (just like arrays)
- Add elements
- Remove elements
Remember how we had to import
java.util.Arrays in order to use additional array methods? To use an
ArrayList at all, we need to import them from Java’s
util package as well:
Let’s learn how to make use of this powerful object…
In Shopping.java we’ve defined two arrays:
We’ve tried to add a new item to the end of each. Run the code — does it work?