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]

Senior Consultant/Information Security - permanent position (TX)
Next Step Systems
US-TX-Irving

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

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

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