| home / programming / java_core / 1 | [previous][next] |
|
|
When creating an array, you can populate (assign elements to) it at the same time by passing the value of the elements as arguments to the Array() constructor. Later on, you can add or delete elements as you wish.
|
|
|---|
|
<html><head><title>The Array Object</title></head> <h2>An Array of Colored Strings</h2> <script language="JavaScript"> 1 var colors = new Array("red", "green", "blue", "purple"); 3 document.write("<font color='"+colors[i]+"'>"); |
|
|
|---|
A new array called colors is created and assigned five colors. |
An associative array is an array that uses a string as an index value, instead of a number. There is an association between the index and the value stored at that location. The index is often called a key and the value assigned to it, the value. Key/value pairs are a common way of storing and accessing data. In the following array called states, there is an association between the value of the index, the abbreviation for a state (e.g., "CA"), and the value stored there--the name of the state (e.g., "California"). The special for loop can be used to iterate through the elements of an associative array.
|
|
|---|
|
<html><head><title>Associative Arrays</title></head> <h2>An Array Indexed by Strings</h2> 1 <script language="JavaScript"> 3 states["CA"] = "California"; document.write("The index is:<em> "+ i ); |
|
|
|---|
The JavaScript program starts here. |
|
| home / programming / java_core / 1 | [previous][next] |
| ||||||||||||||||||||
Created: March 27, 2003
Revised: November 19, 2003
URL: http://webreference.com/programming/java_core/1