A list is only as strong as its weakest link. [Donald Knuth]
An ArrayList is an object like an array. We need to declare a reference variable that will store a reference to our ArrayList object.
Declares a reference variable named lottoNumbers of the type ArrayList.
Now that we have a reference variable of the type ArrayList, we can go ahead and create a new ArrayList object and assign it to this variable.
We need to create a new ArrayList object and assign it to our reference variable in order to initialize the variable. Essentially, we need our reference variable to point to some ArrayList object instead of pointing at nothing.
lottoNumbers = new ArrayList();
Creates a new ArrayList object and assigns it to the reference variabel lottoNumbers.
ArrayList lottoNumbers = new ArrayList();
Optionally, like our primitive variables or arrays, we can do this in one line.
Notice the ArrayList did not specify how many, or what type of data it will hold. This is because an ArrayList is both size and type agnostic, meaning that it doesn't care. These are some of the great advantages an ArrayList has over an array. However with more advantages comes some additional work.
Adds the literal value 2 to the last position in the ArrayList.
The ArrayList was empty. Now 2 is only element in the list.
Adds the literal value of 4 to the last position in the ArrayList.
What position is 2 in now?
lottoNumbers.add(8); lottoNumbers -> [ 2, 4, 8 ]
lottoNumbers.add(16); lottoNumbers -> [ 2, 4, 8, 16 ]
lottoNumbers.add(32); lottoNumbers -> [ 2, 4, 8, 16, 32 ]
The only way to add elements to an ArrayList is at the back, or last position. This requires some preparation in a program if the order of elements is important.
Remember how ArrayLists are type agnostic? Well if the ArrayList doesn't care what types of data it contains, how would we go about using that data. Let's take a look at how we get elements out of an ArrayList.
ArrayList stuff = new ArrayList();
So now the ArrayList has four elements, two string literals and two integers.
Let's assume we wanted test the strings for equality. We need some way of getting objects out of the ArrayList and preserving their type. Let's take a look at how this is done.
String firstElement = (String) stuff.get(0);
This will get the first item that was added to the ArrayList and cast it as a String, then assign it to the String variable named firstElement.
String secondElement = (String) stuff.get(1);
Here we've done the same thing with the second element in the ArrayList.
Now that we safely extracted our String elements, cast them as Strings, and assigned them to some variables, we can use the methods of the String datatype to check if they are equal.
There is a large problem though. Say we tried to compare the elements at positions 2 and 3. These are not String literals, they are integers, so our casting to a string will not work. It is this reason that using ArrayLists requires some extra planning on the part of the programmer. You should know what types you have put in each list and how to treat them safely when getting elements back out.