Arrays#
In programming, we often want many of something. Here are your fifteen opponents in your game. Here is a list of all 30 students in this course. Over there I want to draw 1700 stars for my starry night background. While you could create separate variables for two or three things, this would quickly get tedious for tens or hundreds of them. So, what do we do?
You already know one kind of Array, You just don't know it yet. Strings! Strings solve a similar problem. You actually want to collect a bunch of characters and treat them as one thing! So why not collect a bunch of numbers and treat them as one thing?
Your Cabinet Full of Stuff#
If we think about a variable as a drawer with a label on it and something in it, let's extend that metaphor to a whole cabinet of drawers!
All of these would now share a single name. If you put something in drawer 15, it will now live in drawer 15 and you can get it back from drawer 15. In code it could look like this:
Declaring an Array#
There are now four numbers stored in favoriteNumbers
. There are two new things in this code snippet: []
at the end of float[]
tell the computer "this is not just one, but a whole bunch of floats". The variable name looks pretty standard, but one key detail I would like to suggest is: name arrays the plural form of something (numbers in this example or opponents or stars …). We then assign something to our new variable. This is a list of numbers, each separated by a ,
comma. All of them are then included in curly braces {
and }
. In this case, the curly braces are not a block of code, but a list of things.
Look for the Square brackets
Congratulations!
You are now using all the brackets and braces on your keyboard!
() {} []
Whenever you see square brackets, it is a good possibility that arrays are involved.
How would you declare an array of integer numbers?
How would you declare an array of Strings?
Using an Array: Reading#
But, how do we use that variable now?
What do we look for?
Square Brackets!
- Yes, you can absolutely have variables like
PI
in there, if the type is the correct one!
Zero, Again
Yes, Programmers really really like to start counting at zero. This is yet another place where that's usually true. Arrays are also a great place to have an off-by-one error!
So, to access any of the things in your array-variable-cabinet, you use its name favoriteNumbers
and then decide which drawer to open, for example [0]
. We call this number the index at which something is stored in the array. Likewise you could use favoriteNumbers[1]
(index 1) to get to the next item in the list, or favoriteNumbers[2]
(index 2) to the third one ... and so on. One of the upsides is, that you store things together that belong together.
Index Out of Bounds
What were to happen if we wrote favoriteNumbers[27]
in the example above? This is clearly an index that does not exist in a list of four numbers.
Java would throw an ArrayIndexOutOfBoundsException
- that index does not exist, it is too large. It is "out of bounds".
Using an Array: Writing#
That array behaves like a bunch of variables at once, so we can also change the values in it. For this, we're combining the syntax of accessing an element (favoriteNumbers[2]
) and the way we always assigned new values (=
):
float[] favoriteNumbers = {5, 12.5, 417, PI};
favoriteNumbers[2] = 7.0;
println(favoriteNumbers[2]); // now prints 7.0
// Friendship ended with the number 417. 7 is now my best friend.
Relevant excerpt from Learning Processing#
(the section starts at 3:12:29 and runs through 3:28:02, the video should start and stop on these automatically.)