ArrayList
ArrayList
is like a regular Java array on steroids. It overcomes some of the shortfalls of arrays, such as having to predetermine the size. It adds some useful methods to make its data easy to manage and it uses an enhanced version of a for
loop that is clearer to use than a regular for
loop.
Let's look at some code that uses an ArrayList
instance:
// 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 like this code shows:
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
instance this time:
// declare and initialize a new ArrayList ArrayList<String> myList = new ArrayList...