spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java_core / 1 To page 1current pageTo page 3To page 4To page 5To page 6To page 7
[previous][next]

Data Center Architect
The Computer Merchant, Ltd
US-MA-chelsea

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


JavaScript by Example: JavaScript Core Objects. Pt. 1

Populating an Array with a for Loop

Populating an array is the process of assigning values to it. In quigley09.htm#62747, the for loop is used to fill an array. The initial value of the index starts at zero; the looping will continue as long as the value of the index is less than the final size of the array.

 

 

<html><head><title>The Array Object</title><body>

<body>

<h2>An Array of Numbers</h2>

<script language="JavaScript">

1 var years = new Array(10);

2 for(var i=0; i < years.length; i++ ){

3 years[i]=i + 2000;

4 document.write("years[" + i + "] = "+ years[i]

+ "<br>");

}

</script>

</body>

</html>

 

The Array() constructor is called to create a 10-element array called years.
  1. The for loop starts with an initial value of i set to 0, which will be the value of the first index in the array. As long as the value of i is less than the length of the array, the body of the loop will be executed. Each time through the loop, i is incremented by 1.

  2. The array is populated here. Each time through the loop, years[i] is assigned the value of i + 2000.

  3. The value of the new array element is displayed for each iteration of the loop. (See Figure 9.3.)

 
Creating and Populating an Array Simultaneously

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>

<body>

<h2>An Array of Colored Strings</h2>

<script language="JavaScript">

1 var colors = new Array("red", "green", "blue", "purple");

2 for(var i in colors){

3 document.write("<font color='"+colors[i]+"'>");

4 document.write("colors[" + i + "] = "+ colors[i]

+ "<br>");

}

</script>

</body>

</html>

 

 
A new array called colors is created and assigned five colors.
  1. The special for loop iterates through each element of the colors array, using i as the index into the array.

  2. The color of the font is assigned the value of the array element.

  3. The value of each element of the colors array is displayed. The color of the font matches the value. (See Figure 9.4.)

 

Each string is a different font color. Output from See .

Associative Arrays

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>

<body>

<h2>An Array Indexed by Strings</h2>

1 <script language="JavaScript">

2 var states = new Array();

3 states["CA"] = "California";

states["ME"] = "Maine";

states["MT"] = "Montana";

4 for( var i in states ){

document.write("The index is:<em> "+ i );

document.write(".</em> The value is: <em>" + states[i]

+ ".</em><br>");

}

</script>

</body>

</html>

 

 
The JavaScript program starts here.
  1. The Array() constructor is called and returns a new Array object called states.

  2. The index into the array element is a string of text, "CA". The value assigned is "California". Now there is an association between the index and the value.

  3. The special for loop is used to iterate through the Array object. The variable, i, represents the index value of the array, and states[i] represents the value found there. It reads: For each index value in the array called states, get the value associated with it. (See Figure 9.5.)

 

home / programming / java_core / 1 To page 1current pageTo page 3To page 4To page 5To page 6To page 7
[previous][next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: November 19, 2003

URL: http://webreference.com/programming/java_core/1