Java Reference
In-Depth Information
Next, you use a series of document.write() functions to insert the values that each element of the array
contains into the web page. Here the array is out of order just to demonstrate that you can access it that
way:
document.write("myArray[0] = " + myArray[0] + "<br/>");
document.write("myArray[2] = " + myArray[2] + "<br/>");
document.write("myArray[1] = " + myArray[1] + "<br/>");
You can treat each particular position in an array as if it's a standard variable, so you can use it to do
calculations, transfer its value to another variable or array, and so on. However, if you try to access the
data inside an array position before you have defined it, you'll get undefined as a value.
Finally, you changed the value of the second array position to "Mike" . You could have changed it
to a number because, just as with normal variables, you can store any data type at any time in each
individual data position in an array:
myArray[1] = "Mike";
Now your array's contents look like this:
index
data stored
0
Jeremy
1
Mike
2
John
Just to show that the change you made has worked, you use document.write() to display the second
element's value:
document.write("myArray[1] changed to " + myArray[1]);
a multi‐dimensional array
Suppose you want to store a company's personnel information in an array. You might have data
such as names, ages, addresses, and so on. One way to create such an array would be to store the
information sequentially—the first name in the first element of the array, then the corresponding age
in the next element, the address in the third, the next name in the fourth element, and so on. Your
array could look something like this:
index
data stored
0
Name1
1
Age1
2
Address1
continues
 
Search WWH ::




Custom Search