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. Casinos that make ordinary people rich.

    viva9988 The website is open to the service of joy. Online gambling is not for fun anymore, because whenever a gambler enters a game of online gambling, the gambler will play the gambler. Know that generating income from playing gambling online is very easy.

    If you are a gambler, you will know that making money from playing online casino games is not difficult for you to play online gambling games. There are both new pleasures and new online gambling that will allow players to experience the fun and open up the gambling experience with global gamblers. Eldon

    By playing online casino games in the present day, most popular players love to play online casino games through the broadcast system. The new game that makes playing online casino games more interesting and fun, and now the player can play online gambling games all day and all night playing through the web. จีคลับ

    ReplyDelete
  2. Thanks for sharing, nice post! Post really provice useful information!

    Công ty vận chuyển hàng nước ngoài FadoExpress, hàng đầu chuyên vận chuyển, chuyển phát nhanh siêu tốc đi khắp thế giới, nổi bật là dịch vụ gửi hàng đi mỹ, gửi hàng đi nhậtgửi hàng đi pháp và dịch vụ chuyển phát nhanh đi hàn quốc uy tín, giá rẻ

    ReplyDelete