Arrays go back a while to the roots of Java in languages like C. It's important to know they exist, but they're a bit limited and clunky, and have had their day, like a VHS copy of Friends.
ArrayList is much more flexible, and has a ton of useful features built into it. You can basically loop through, add, remove elements as you need.
My co-worker Dan says "why would you EVER need to use arrays again?".
You can declare an ArrayList as ...
ArrayList<variable> listName = new ArrayList <variable>;
- Variable is the kind of variable you want to use.
- listName is the name of the list you're using [I'm starting to sound like an ISTQB definition here]
So to declare String, int or variable ArrayLists is ...
ArrayList<String> listName = new ArrayList <String>;
ArrayList<int> listName = new ArrayList <int>;
ArrayList<doube> listName = new ArrayList <double>;
Notice with all of these, we didn't need to specify a size as we went.
We can then for instance add elements as we go by ...
For each .add, listName expands as it adds.
Likewise we can decide that having double "Pugh" is a mistake (it isn't), and so remove an element with,
Which will remove the element at 1 (remember in array counting, this is actually the 2nd item).
We can also use the .get(n) command to find the value at the nth position in the list. So,
After our remove will return "Barney McGrew".
Finally, and what's ever so useful, we can use a special version of for with ArrayLists,
for( var varName : listName )
This will step through each item in the ArrayList, where
- var is the type of variable in the ArrayList
- varName is the name of an instance
- listName is the name of the ArrayList
Basically for each loop, varName is assigned to the value of the next element in the list, until you've reached the end of the list.
So for our modified rollcall,
for( string surname : listName )
If you're wondering why these might sound familiar ...
Back to our competition
I decided to have two winners for our competition - one selected by Array, the other by ArrayList.
[To use ArrayList, don't forget to import java.util.ArrayList and import java.util.List;]
Here you can see I declare an ArrayList of entrants of type List. I commented out @miller_alaine, as she won last time.
I can find the size of my list using the entrants.size() method.
I get a random number winnerNum, and use entrants.get(winnerNum) to get the Twitter handle of the list.
Something I couldn't do before - I can then remove the winner from my list with entrants.remove(winnerNum).
I then look though the Twitter of the remaining entrants to get a list of those who go home empty handed (sorry folks).
When I run this code, I get ...
Well done Sandeep!
The code for this is here in Github.
It's well worth reading more about ArrayLists. Google around to find other sources of information.
Why do we declare List and not ArrayList?
I've had one of our senior Java developers make a suggestion to this code, which I've since implemented.
Originally I declared my ArrayList as an ArrayList type,
ArrayList<String> entrants = new ArrayList<String>();
However I was told this is considered bad practice, and normally they use,
List<String> entrants = new ArrayList<String>();
You can read up why here. But in a nutshell, ArrayList is a kind of List, but if you use ArrayList in the declaration, you lose the interface to some List features and flexibility.