ArrayLists

ArrayList is like a regular Java array on steroids. It overcomes some of the shortfalls of arrays such as having to predetermine its size. It adds some really useful methods to make its data easy to manage, and it uses an enhanced version of a for loop, which is clearer to use than a regular for loop.

Let's look at some code that uses ArrayList:

// Declare a new ArrayList called myList to hold int variables
ArrayList<int> myList;
 
// Initialize the myList ready for use
myList = new ArrayList<int>();

In the previous code, we declared and initialized a new ArrayList called myList. We can also do this in a single step as shown in the following code:

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

We have seen nothing especially interesting so ...

Get Android Programming for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.