ArrayLists
An 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 a number of 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 demonstrated by the following code:
ArrayList<int> myList = new ArrayList<int>();
Nothing especially interesting so far, so let's take a look at what we can actually do with ArrayList
. Let's use a String ArrayList
this time:
// declare and initialize a new ArrayList ArrayList<String> myList = new ArrayList<String>...