Wednesday, August 31, 2016

JAVA 10 - Arrays

Way back in JAVA 4 we looked at declaring and using variables - and we've been using them ever since.



Sometimes though we need a multiple grouping of an item.  For instance, name - you have your own name obviously.  But remember back in school and your teacher called a register for class to see who was present?

For something like that you need a grouping of names ... which we call an array.

Here are a couple of examples,

String[] rollcall = { "Pugh", "Pugh", "Barney McGrew", "Cuthbert", "Dibble", "Grubb" };

Here,

  • the [] square brackets declare we're calling an array of Strings.  
  • rollcall is the name of the array
  • the {} brackets hold our list of items
  • "Pugh" - each item in quotes is an item in our array - we have 6 in all


Elsewhere, we can extract an item from our array by going ...

name[index]

  • name - is the name of the array, obviously
  • index - is the nth number in the array.  Now an important thing to know about it index is it starts from 0.


So,

  • rollcall[0] returns our first item "Pugh"
  • rollcall[5] returns our last item "Grubb"
  • rollcall[6] produces an error

You could also declare arrays for double or ints,

int[] eyesFingerKneesToes = { 2, 10, 2, 11 };

double[] percentComplete = { 22.3, 30.2, 82,1 };

You can also declare an array as empty, and then add values to it later ...

eyesFingerKneesToes = new int[4];
eyesFingerKneesToes[0] = 2;
eyesFingerKneesToes[1] = 10;
eyesFingerKneesToes[2] = 2;
eyesFingerKneesToes[3] = 11;

What's important to know though - once you've declared your array, you can't make it bigger.  It's a bit of a limitation of arrays, but next time we'll talk about ways around it.

Competition Draw

Here's an example I know you've been dying to see - yes, I'm using software for my competition draw!

Building on some unit testing we did a while ago, I've a class (shhh ... spoilers) which allows me to choose a random number.

Here's the JUnit test though I use this to do my draw ...


Here I declare an array of entrants who replied to my blog.

I can access methods and data within the entrants array, so when I choose entrants.length, it tells me the number of entrants in that array (I know I could count, but this is more fun).

I use that to define the maximum number I want, and get a random number winnerNum back.

I then reference the Twitter handle of the winner by using entrants[winnerNum].

From this we clearly see ...

Well done Alaine Miller!

Was it a fair draw?

I actually had another check inside my JUnit tests - which you might remember from looking at unit tests here.

This function is a much more complex in it's use of loops and arrays ...


This runs 800,000 draws for 8 entrance, making sure they're all within 1% of each other.  It does this by creating countArray - where it counts the number of 0, 1 ... 8 results.  So countArray[4] tells how many times the number 4 has come up etc.

I loop through 800,000 rolls, then loop through the values 0 ... 8 to make sure each is about 1/8 of the number of selections.

That's as fair as I can get it!

You can find the code for this here.


Extension material

Read more about arrays here.

If experimenting around with this yourself, you will need to import java.util.Arrays.

No comments:

Post a Comment