All these objects and variables are useful but what happens when we have hundreds or thousands of them? How can we possibly keep track? Java arrays are the solution and in this quick tutorial, we will see the simple way that we can organize objects and variables into arrays.
[widgets_on_pages id=”udemy_advert_java_1″][widgets_on_pages id=”udemy_code_details”]
To get started look at this potential nightmare scenario where we have a high score table with a hundred scores in it.
int highScore1; int highScore2; int highScore3; //96 more lines like the above int highScore100;
Straight away this can seem unwieldy but what about when someone gets a new high score and we have to shift the scores in every variable down one place. The nightmare begins.
highScore100 = highScore99; highScore99 = highScore98; highScore98 = highScore97; //96 more lines like the above highScore1 = score;
Now imagine we want to keep track of String variables that represent the name of each person who achieved each high score. Clearly, we have a problem. There must be a better way. When we have a whole array of variables what we need is a Java array. An array is a variable that holds up to a predetermined maximum number of other variables. Each element is a variable of a consistent type.
The following code declares an array that can hold
int type variables; perhaps even a high-score table.
int [] highScores;
We could also declare arrays of other types like this.
String [] playerNames; boolean [] liveAliens; float [] bulletVelocities;
Each of these arrays would need to have a fixed maximum amount of storage space allocated before it was used. We do so like this.
intArray = new int [100];
The above allocates up to a maximum of 100 int sized storage spaces. Arrays can seem slightly strange to new programmers because the storage spaces start off at zero, not 1. Therefore in a 100 wide array, the storage spaces would run from 0 to 99. We could actually initialize some of these storage spaces like this.
intArray[0] = 5; intArray[1] = 6; intArray[2] = 7;
But note we can only ever put the declared type into an array and the type that an array holds can never change.
intArray[3]= "Jeff Minter";// Won't work
So when we have an array of int types, what are each of the int variables called- what are their names? The array notation syntax replaces the name. And we can do anything with a variable in an array that we could do with a regular variable with a name.
// Assign a value to the int stored at position 3 intArray [3] = 123; // Subtract the value stored in position // 4 from the value stored in position 9 // and assign it to position 10 intArray[10] = intArray[9] - intArray[4]; // Declare a regular int variable called myInt //and assign it the value held in position 3 int myInt = intArray [3];
Note that myInt is a completely separate and distinct variable and any changes to it do not affect the value stored in the intArray. Think of an array variable as an address to a load of whatever type variables. Perhaps, using a warehouse analogy someArray is an aisle number. So someArray[0], someArray[1] and so on are the aisle number followed by the position number in the aisle.
Did I mention that Arrays are objects?
Arrays are actually objects. That is they have methods and variables of their own which we can use. For example:
int lengthOfSomeArray = someArray.length;
Above we assigned the length (how many elements are stored) of someArray to the int variable called lengthOfSomeArray. We can even declare an array of arrays. This is an array that, in each of its elements, lurks another array. If this makes your head explode don’t worry we won’t be needing to do this to build our first few games. Here is how we declare an array of arrays:
String[][] countriesAndCities;
In the above array, we could hold a list of cities within each country. Just remember that an array holds up to a predetermined number of variables of any predetermined type and those values are accessed using this syntax:
someArray[someLocation];
[widgets_on_pages id=”udemy_advert_java_3″][widgets_on_pages id=”udemy_code_details”]
Now we have just two more topics to cover and then we can make our first real game. There will be lots of things going on in our games so we need to be able to manage simultaneous events with Threads.
Wow, thanks this really helped. I have recommended this set to the students in my programming class and it seemed to of helped them too! Thanks again.
Many thanks Jake.
Thank you very much. I cant went to school anymore. And this site maked me learn what i can got on my school too. Hopefully to post more tutorial java and more code cause i love that