Java Reference
In-Depth Information
personnel[2] = [];
personnel[2][0] = "Name2";
personnel[2][1] = "Age2";
personnel[2][2] = "Address2";
The array now looks like this:
index
0
1
2
0
Name0
Name1
Name2
1
Age0
Age1
Age2
2
Address0
Address1
Address2
You have now finished creating your multi‐dimensional array. You end the script block by accessing
the data for the second person ( Name1 , Age1 , Address1 ) and displaying it in the page by using
document.write() . As you can see, accessing the data is very much the same as storing it. You can
use the multi‐dimensional array anywhere you would use a normal variable or single‐dimension
array.
document.write("Name : " + personnel[1][0] + "<br/>");
document.write("Age : " + personnel[1][1] + "<br/>");
document.write("Address : " + personnel[1][2]);
Try changing the document.write() commands so that they display the first person's details. The code
would look like this:
document.write("Name : " + personnel[0][0] + "<br/>");
document.write("Age : " + personnel[0][1] + "<br/>");
document.write("Address : " + personnel[0][2]);
It's possible to create multi‐dimensional arrays of three, four, or even a hundred dimensions, but things
can start to get very confusing, and you'll find that you rarely, if ever, need more than two dimensions.
To give you an idea, here's how to declare and access a five‐dimensional array:
var myArray = [];
myArray[0] = [];
myArray[0][0] = [];
myArray[0][0][0] = [];
myArray[0][0][0][0] = [];
myArray[0][0][0][0][0] = "This is getting out of hand";
document.write(myArray[0][0][0][0][0]);
That's it for arrays for now, but you return to them in Chapter 5, where you'll find out something
shocking about them. You also learn about some of their more advanced features.
Search WWH ::




Custom Search