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 far, so let's take a look at what we can actually do with ArrayList
. Let's use String ArrayList
this time:
// declare and initialize a new ArrayList ArrayList<String> myList = new ArrayList<String>(); // Add a...