Java Reference
In-Depth Information
(continued)
index
data stored
3
Name2
4
Age2
5
Address2
6
Name3
7
Age3
8
Address3
This would work, but there is a neater solution: using a multi‐dimensional array . Up to now you
have been using single‐dimension arrays. In these arrays each element is specified by just one
index—that is, one dimension. So, taking the preceding example, you can see Name1 is at index 0 ,
Age1 is at index 1 , and so on.
A multi‐dimensional array is one with two or more indexes for each element. For example, this is
how your personnel array could look as a two‐dimensional array:
index
0
1
2
0
Name1
Name2
Name3
1
Age1
Age2
Age3
2
Address1
Address2
Address3
You see how to create such multi‐dimensional arrays in the following “Try It Out” section.
a Two‐Dimensional array
trY it out
This example illustrates how you can create such a multi‐dimensional array in JavaScript code and how
you can access the elements of this array. Type this code and save it as ch2 _ example9.html :
<!DOCTYPE html>
<html lang = "en">
<head>
<title>Chapter 2, Example 9</title>
</head>
<body>
<script>
var personnel = [];
personnel[0] = [];
personnel[0][0] = "Name0";
personnel[0][1] = "Age0";
Search WWH ::




Custom Search