Click here for the previous tutorial.
An array is a set of variables of the same type. They can exist for any type of variable, from primitive types to the most complex of objects. The declaration is quite similar to that of normal variables.
int intArrayName[];
intArrayName[0] = 5;
In this case, an array was created using type int (the brackets signify that is an array, and not just an int) and the value of the first elemetn was set to 5. Recall that computers love nothing more than starting at zero. Alternatively, if you wanted to set a large number of values quickly, that could be done at the declaration of the array.
int intArrayName[] = {5, 6, 3, 5, 3};
Note that this can only be done during declaration.
To assign values to a specific element, or to retreive from one storage location, use intArrayName[(location number)] as if it was a regular int.
This works for more than just integers. Incase you remember a promise of usefulness of the char type, it is a bit easier to explain now. Let us quickly make a map print to the console, using ~, ^ and space as promised.
We can make a stright line, using
char charArrayName[] = {' ', ' ', '~', '^'};
and then make a while loop to print this to the console.
int counter = 0;
while(counter < charArrayName.length){
System.out.print(charArrayName[counter]);
counter++;
}
A few words about our previous section of code before moving on. First, you may have noticed we used System.out.print instead of println. This is becasue println will make a new line when done printing. print works exactly the same, except the next thing to display will stay on the same line. This allows us to print the map more accurately to the screen. Also, the way we currently have it set up, it would take a bit of work to actually make a map that goes both horizontally and vertically. If you have not been able to figure out how to display one yet, the problem is not you, but the type of array we are using. We need to use something called a multideminsional array, and nested while loops. This might all sound complex, but it should clear right up soon.
First off, a nested while loop is just a fancy term for a while loop inside of another while loop. This can be done for any loop, as well as if statements, switches, and anything else.
A multi-deminsional array is not really as complex as it may sound, either. Essentially, it is an array of arrays. This will probably make more sense if given an example, so let's just go ahead and declare one.
char mapArray[][] = {{' ', ' ', ' ', '^', '^'},
{'~', '~', '~', '~', '^'},
{' ', ' ', ' ', '~', '~'},
{' ', ' ', ' ', ' ', '^'},
{' ', ' ', ' ', ' ', '^'}};
Now a map is created, and all stored under the mapArray variable. All we need then is an algorith to read it and print it to the screen. To do this, we can make a while loop to run through the horizontal lines, using a print statement, and then another one that determines when to jump to the next line, using a println statement. It is not as complicated as it may sound.
int x = 0;
int y = 0;
while(y < 5){
while(x < 5){
System.out.print(mapArray[y][x]);
x++;
}
x = 0;
System.out.println();
y++;
}
Click here to coninue to the next tutorial.
Sample code explained here:
while(y < 5){
while(x < 5){
System.out.print(mapArray[y][x]);
x++;
}
x = 0;
System.out.println();
y++;
}
int intArrayName[] = {5, 3, 3, 5};
char mapArray[][] = {{' ', ' ', ' ', '^', '^'},
{'~', '~', '~', '~', '^'},
{' ', ' ', ' ', '~', '~'},
{' ', ' ', ' ', ' ', '^'},
{' ', ' ', ' ', ' ', '^'}};
Always remember to keep code readible!
Often times when making multideminsional arrays, it is a good idea to include some extra white space in order to align columns. This will prove especially useful in things like maps, as shown here.
Loops and tests can be infinitely nested in Java. This means you can have a while loop inside a while loop inside a while loop, and go as deep as you would like. This is often unavoidable, so just make sure to keep them as simple and neat as possible when doing this, to add to productivity.