Wednesday, August 31, 2016

JAVA 11 - Array ... list. ArrayList!

Didn't we do this already?  No!  Last time we looked at arrays, and today we're looking at array lists.

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>;

Where,

  • 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 ...

listName.add("Pugh");
listName.add("Pugh");
listName.add("Barney McGrew");
listName.add("Cuthbert");
listName.add("Dibble");
listName.add("Grub");

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,

listName.remove(1);

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,

listName.get(1)

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 )
{
    System.out.println(surname);
}

Will produce,

Pugh
Barney McGrew
Cuthbert
Dibble
Grub

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.

Extension material

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.

2 comments:

  1. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    PHP Training in Chennai

    ReplyDelete
  2. Nicely explained. Here you described the well written article from your in-depth knowledge. Truly impressive and nice information

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete