1. Playing
2. Eating
3. Sleeping
These three things could easily be categorized into one list so that you can refer people to this list rather than three separate variables. That's what an array does, in principle.
1. To see this in action, create a new Project, and name it whatever you want.
2. Within the src folder, create a new Java class called: Array.
3. Add the main method:
public static void main(String args[]) {
}
4. Within the main method, declare the following:
String[] favoriteThings = new String [3];
This creates an Array (list) of String objects called stringArray, and allocates memory to accommodate three (3) items.
Alternatively, this same line can be written as below, by shifting the brackets:
String favoriteThings[] = new String [3];
Astute observers will recognize this syntax. If you examine the parameter of the main method:
String args[], you will now see that the main method takes in an array of Strings as a parameter. These arrays are passed in at a lower level, and you will not encounter them in Eclipse.
Your code should be looking like this:
Figure 2-34: Array Class
public static void main(String args[]) {
String[] favoriteThings = new String [3];
}
}
We can refer to positions in the array as indices. The first item in the list will have an index of 0, the second will have 1, and so on.
To refer to a specific index of an Array, we can write:
array[index].
Going back to our code, we can list our three favorite things as follows:
...
public static void main(String args[]) {
String[] favoriteThings = new String [3];
favoriteThings[0] = "Playing";
favoriteThings[1] = "Eating";
favoriteThings[2] = "Sleeping";
}
...
What if we now want to print all these items?
It's tempting to use System.out.println(favoriteThings), but this will return what seems to be nonsense.
Instead, we must use a for loop, and iterate over an index to print each line.
This is how I would approach it:
for (int i = 0; i < favoriteThings.length; i++) {
System.out.print(i+1 + ". ");
System.out.println(favoriteThings[i]);
}
This code introduces nothing new. It is just application of previously-covered topics. Make sure you understand this before moving on!
If we place this for loop at the bottom of the main method, we will see the following output:
For our game's purposes, we will use a 2D array to hold a value at each position, and then paint the object/character/platform associated with said value at its (x, y) coordinate.
2D Arrays are very similar to single dimensional arrays, but we add a second dimension (like adding a width).
Two create a 2D array, we simply state:
int[][] intArray = new int[30][50]; //Creates a 2D array of integers with height 30 and width 50.
1. Create a new class called Tilemap.
2. Add the following code:
Figure 2-35: Tilemap Class
public class Tilemap {
public static void main(String args[]) {
Tilemap tp = new Tilemap();
}
public Tilemap() {
int[][] tilemap = new int[30][50];
System.out.println("New Tilemap created.");
Random r = new Random();
int rows = tilemap.length;
int columns = tilemap[1].length;
printTiles(rows, columns, tilemap, r);
}
private void printTiles(int rows, int columns, int[][] tilemap, Random r) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
tilemap[i][j] = r.nextInt(5);
System.out.print(" " + tilemap[i][j]);
}
System.out.println("");
}
}
}
1. We import the Random class (to be used later)
2. In the main method, we create a new Tilemap object called tp. This calls the constructor below the main method.
3. In the constructor, we create a new two dimensional array called tilemap, with height 30, and width 50.
- We also create a Random object called r
Two-dimensional arrays can be thought of as Arrays of Arrays. Meaning that it is a list of lists. The larger list has 30 empty spots. Each of those 30 spots has 50 spots across.
Therefore, when we refer to tilemap.length, we get the length of the larger list, which is 30.
When we refer to tilemap[1].length, we get the length of the small list with index 1 in the large list, which is the second small list in the large list. This will have a value of 50.
4. We then call the printTiles() method, which takes in various parameters, uses two for loops (one for the columns and another for the rows).
This printTiles() method gives each space a value between 0 to 4 (at random). Then we print each row and column.
The result should look something like this:
Stay Tuned!