• $10 Game Dev' Courses - Use code "BREAKTHRU"

    Video courses

The Complete Java Developer Course. Learn Step by Step

The Complete Android & Java Developer Course – Build 21 Apps

Master Android N App Development With Java

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.

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;

  • Learning java by building Android games

    Learning Java by Building Android Games

    Want to learn Java for Android? Even if you are completely new to either Java, Android, or game programming but are aiming to publish Android games, then this book is for you. This book also acts as a refresher for those who already have experience in Java on other platforms or other object-oriented languages.
    • Setup your own Android game programming environment using Android Studio
    • Control logic, branch your code, and add real decision-making depth to your games
    • Design and use 2d sprite animations, smooth pixel graphics, sound FX, simple collision detection and artificial intelligence (AI)
    • Build around a dozen sample test apps and 4 complete working games!
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)

    Android Game Programming by Example

    Learn to build exciting Android game projects. Build a simple flappy-bird like game to a multi-environment, tough, retro platformer then an OpenGL ES 2 Asteroids clone, running at hundreds of frames per second .
    • Animate your characters with sprite sheets, add multiple scrolling parallax backgrounds, and implement genuinely tough, playable levels in your games
    • Every single line of code is printed in the book! No need to refer to the code files while trying to follow along. All the code files are also supplied separately so you can refer to them in their completed form and copy/paste them into your project if you like.
    • Implement a multitude of other game features such as pickups, firing weapons, HUD’s, generating and playing sound FX, scenery, level transition, high scores, and more
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US Only)

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:


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.