Java Reference
In-Depth Information
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 specifi ed 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'll see how to create such multi-dimensional arrays in the following “Try It Out” section.
Try It Out A Two-Dimensional Array
The following 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 the code and save it as ch2_examp9.htm.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<body>
<script type=”text/javascript”>
var personnel = new Array();
personnel[0] = new Array();
personnel[0][0] = “Name0”;
personnel[0][1] = “Age0”;
personnel[0][2] = “Address0”;
personnel[1] = new Array();
personnel[1][0] = “Name1”;
personnel[1][1] = “Age1”;
personnel[1][2] = “Address1”;
personnel[2] = new Array();
personnel[2][0] = “Name2”;
personnel[2][1] = “Age2”;
personnel[2][2] = “Address2”;
document.write(“Name : “ + personnel[1][0] + “<BR>”);
document.write(“Age : “ + personnel[1][1] + “<BR>”);
document.write(“Address : “ + personnel[1][2]);
</script>
Search WWH ::




Custom Search