Fun with arrays

This is an introduction to arrays. Arrays are used to store multiple values in a single variable and can be single or multi-dimensional (2d, 3d, etc). Arrays are a little tricky to understand for early coders, so this tutorial should help make it a little easier.

Arrays are 0-index based, meaning the "first" object is assigned 0, "second" object is 1, etc.



Single array

To declare an array, define the variable type with square brackets:
String[] teams;

Declaring it this way, no array object gets created and the 'teams' variable is simply declared without value. This is not recommended, but is useful if you know you need an array but won't know it's size until later.

To assign the 'teams' variable a size and populate it you can do the following:

teams = new String[4];

teams[0] = "Titans";
teams[1] = "Texans";
teams[2] = "Jaguars";
teams[3] = "Colts";

//You can also declare and initialize all at once like this:
String[] teams = {"Titans", "Texans", "Jaguars", "Colts"};

We can do the same thing with other data types, such as int: int[] intArray = new int[] {2,4,6,8,10};

Looping over a String array:

To find out how many elements an array has, use the length property. While looping our string array we can use .length to loop until we have no more teams

String[] teams = {"Titans", "Texans", "Jaguars", "Colts"};

for (int i=0; i < teams.length; i++)
{
	System.out.println(teams[i]);
}

//Using System.out we printed each item in the array.
//Output below:
Titans
Texans
Jaguars
Colts

Multidimensional arrays

A multidimensional array is just an array containing more than one array; 2d = 2 arrays, 3d = 3 arrays, etc...
As such, each array can very in the number of columns it holds. Examples of declaring 2d and 3d array:

2d array: int[][] a = new int[3][4];
3d array: int[][][] b = new int[3][4][2];

The image above shows the breakdown of our 2d array example.

Two dimensional (2d) arrays

Think of the 2d array in this way: int[][] a = new int[rows][columns];
This 2d array can hold 12 items (3*4).

Let's create a 2d array:



So how do we get the value we want from the array?

//Get the value from the array (the one depicted above)
int x = a[0][0];  //x = 4
int y = a[2][2];  //y = 23
int z = a[0][3];  //z = 1;


//Lets loop over the array and print all the values
int[][] a = { { 4,0,15,1 }, { 7,8,42,6 } , { 9,-2,23,9} }; 

for (int i = 0; i < a.length; i++) {
	for (int j = 0; j < a[i].length; j++) {
		System.out.println("a[" + i + "][" + j + "] = "+ a[i][j]);
	}
}

//Output
a[0][0] = 4
a[0][1] = 0
a[0][2] = 15
a[0][3] = 1
a[1][0] = 7
a[1][1] = 8
a[1][2] = 42
a[1][3] = 6
a[2][0] = 9
a[2][1] = -2
a[2][2] = 23
a[2][3] = 9

Arrays are helpful in lots of situations. I am developing a game, Blantium, in which I store tiles in an array object. For example, when I load my map I have certain tiles I want to store in a collision array. When I draw my character I want to loop through the array and see if he will collide with any of them. If he does, then I don't want to draw him going in the same direction or it would look like he can walk through objects.

Best of luck!