Array

  • Revision slug: JavaScript/Reference/Global_Objects/Array
  • Revision title: Array
  • Revision id: 27689
  • Created:
  • Creator: 立石
  • Is current revision? No
  • Comment /* Description */

Revision Content

Summary

Core Object

Lets you work with arrays.

Created by

The Array object constructor:

new Array(arrayLength)
new Array(element0, element1, ..., elementN)

An array literal:

[element0, element1, ..., elementN]

JavaScript 1.2 when you specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag:

new Array(element0, element1, ..., elementN)

JavaScript 1.2 when you do not specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag:

new Array([arrayLength])
new Array([element0[, element1[, ..., elementN]]])

JavaScript 1.1:

new Array([arrayLength])
new Array([element0[, element1[, ..., elementN]]]) 

Parameters

arrayLength 
The initial length of the array. You can access this value using the length property. If the value specified is not a number, an array of length 1 is created, with the first element having the specified value. The maximum length allowed for an array is 4,294,967,295.
elementN 
A list of values for the array's elements. When this form is specified, the array is initialized with the specified values as its elements, and the array's length property is set to the number of arguments.

Description

An array is an ordered set of values associated with a single variable name.

The following example creates an Array object with an array literal; the coffees array contains three elements and has a length of three:

coffees = ["French Roast", "Columbian", "Kona"]

You can construct a dense array of two or more elements starting with index 0 if you define initial values for all elements. A dense array is one in which each element has a value. The following code creates a dense array with three elements:

myArray = new Array("Hello", myVar, 3.14159) 

Indexing an array

You index an array by its ordinal number. For example, assume you define the following array:

myArray = new Array("Wind","Rain","Fire")

You then refer to the first element of the array as myArray{{mediawiki.external(0)}} and the second element of the array as myArray{{mediawiki.external(1)}}.

Specifying a single parameter

When you specify a single numeric parameter with the Array constructor, you specify the initial length of the array. The following code creates an array of five elements:

billingMethod = new Array(5)

The behavior of the Array constructor depends on whether the single parameter is a number.

  • If the value specified is a number, the constructor converts the number to an unsigned, 32-bit integer and generates an array with the length property (size of the array) set to the integer. The array initially contains no elements, even though it might have a non-zero length.
  • If the value specified is not a number, an array of length 1 is created, with the first element having the specified value.

The following code creates an array of length 25, then assigns values to the first three elements:

musicTypes = new Array(25)
musicTypes[0] = "R&B"
musicTypes[1] = "Blues"
musicTypes[2] = "Jazz"

Increasing the array length indirectly

An array's length increases if you assign a value to an element higher than the current length of the array. The following code creates an array of length 0, then assigns a value to element 99. This changes the length of the array to 100.

colors = new Array()
colors[99] = "midnightblue"

Creating an array using the result of a match

The result of a match between a regular expression and a string can create an array. This array has properties and elements that provide information about the match. An array is the return value of RegExp.exec, String.match, and String.replace. To help explain these properties and elements, look at the following example and then refer to the table below:

<SCRIPT LANGUAGE="JavaScript1.2">
//Match one d followed by one or more b's followed by one d
//Remember matched b's and the following d
//Ignore case

myRe=/d(b+)(d)/i;
myArray = myRe.exec("cdbBdbsbz");

</SCRIPT>

The properties and elements returned from this match are as follows:

Property/Element Description Example
input A read-only property that reflects the original string against which the regular expression was matched. cdbBdbsbz
index A read-only property that is the zero-based index of the match in the string. 1
{{mediawiki.external(0)}} A read-only element that specifies the last matched characters. dbBd
{{mediawiki.external(1)}}, ...{{mediawiki.external('n')}} Read-only elements that specify the parenthesized substring matches, if included in the regular expression. The number of possible parenthesized substrings is unlimited. {{mediawiki.external(1)}}=bB
{{mediawiki.external(2)}}=d

Backward Compatibility

JavaScript 1.2

When you specify a single parameter with the Array constructor, the behavior depends on whether you specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag:

  • If you specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag, a single-element array is returned. For example, new Array(5) creates a one-element array with the first element being 5. A constructor with a single parameter acts in the same way as a multiple parameter constructor. You cannot specify the length property of an Array using a constructor with one parameter.
  • If you do not specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag, you specify the initial length of the array as with other JavaScript versions.

JavaScript 1.1 and earlier

When you specify a single parameter with the Array constructor, you specify the initial length of the array. The following code creates an array of five elements:

billingMethod = new Array(5)

JavaScript 1.0

You must index an array by its ordinal number; for example myArray{{mediawiki.external(0)}}.

Properties

constructor: Specifies the function that creates an object's prototype.

index: For an array created by a regular expression match, the zero-based index of the match in the string.

input: For an array created by a regular expression match, reflects the original string against which the regular expression was matched.

length: Reflects the number of elements in an array.

prototype: Allows the addition of properties to all objects.

Methods

About Array methods: generic methods

Many methods on the JavaScript Array object are designed to be generally applied to all objects which "look like" Arrays. That is, they can be used on any object which has a length property, and which can usefully be accessed using numeric property names (as with array{{mediawiki.external(5)}} indexing).

TODO: give examples with Array.prototype.forEach.call, and adding the method to an object like JavaArray or String.

Some methods, such as join, only read the length and numeric properties of the object they are called on. Others, like reverse, require that the object's numeric properties and length be mutable; these methods can therefore not be called on objects like String, which does not permit its length property or synthesized numeric properties to be set.

The methods that work on any Array-like object and do not need to alter length or numeric properties are:

The methods that alter the length or numeric properties of the object they are called on are:

About Array methods: iteration and mutation

Several Array methods take as arguments functions to be called back while processing the array (every, filter, map, some, sort). When these methods are called, the length of the Array is sampled, and changes to the Array's length from within the callback are not honoured. Other changes to the Array (setting the value of an element, or deleting an element) may affect the results of the operation if the method visits the changed element afterwards. The specific behaviour of these methods in such cases is not always well-defined, and should not be relied upon.

Method list

concat: Joins two arrays and returns a new array.

filter: Creates a new array with all of the elements of the current array for which the provided filtering function returns true.

forEach: Calls a function for each element in the array.

indexOf: Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.

join: Joins all elements of an array into a string.

lastIndexOf: Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

map: Creates a new array with the results of calling a provided function on every element in this array.

pop: Removes the last element from an array and returns that element.

push: Adds one or more elements to the end of an array and returns the new length of the array.

reverse: Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.

shift: Removes the first element from an array and returns that element.

slice: Extracts a section of an array and returns a new array.

sort: Sorts the elements of an array.

splice: Adds and/or removes elements from an array.

toSource: Returns an array literal representing the specified array; you can use this value to create a new array. Overrides the Object.toSource method.

toString: Returns a string representing the array and its elements. Overrides the Object.toString method.

unshift: Adds one or more elements to the front of an array and returns the new length of the array.

valueOf: Returns the primitive value of the array. Overrides the Object.valueOf method.

Examples

Example: Creating an Array

The following example creates an array, msgArray, with a length of 0, then assigns values to msgArray{{mediawiki.external(0)}} and msgArray{{mediawiki.external(99)}}, changing the length of the array to 100.

msgArray = new Array()
msgArray[0] = "Hello"
msgArray[99] = "world"
// The following statement is true,
// because defined msgArray[99] element.
if (msgArray.length == 100)
   myVar="The length is 100."

Example: Creating a Two-dimensional Array

The following creates a two-dimensional array and assigns the results to myVar.

myVar="Multidimensional array test; "
a = new Array(4)
for (i=0; i < 4; i++) {
   a[i] = new Array(4)
   for (j=0; j < 4; j++) {
      a[i][j] = "["+i+","+j+"]"
   }
}
for (i=0; i < 4; i++) {
   str = "Row "+i+":"
   for (j=0; j < 4; j++) {
      str += a[i][j]
   }
   myVar += str +"; "
}

The following string is assigned to myVar (line breaks are used here for readability):

Multidimensional array test;
Row 0:[0,0][0,1][0,2][0,3];
Row 1:[1,0][1,1][1,2][1,3];
Row 2:[2,0][2,1][2,2][2,3];
Row 3:[3,0][3,1][3,2][3,3];

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p><b>Core Object</b>
</p><p>Lets you work with arrays.
</p>
<h3 name="Created_by"> Created by </h3>
<p>The <code>Array</code> object constructor:
</p>
<pre class="eval">new Array(<i>arrayLength</i>)
new Array(<i>element0</i>, <i>element1</i>, ..., <i>elementN</i>)
</pre>
<p>An array literal:
</p>
<pre class="eval">[<i>element0</i>, <i>element1</i>, ..., <i>elementN</i>]
</pre>
<p>JavaScript 1.2 when you specify <code>LANGUAGE="JavaScript1.2"</code> in the <code>&lt;SCRIPT&gt;</code> tag:
</p>
<pre class="eval">new Array(<i>element0</i>, <i>element1</i>, ..., <i>elementN</i>)
</pre>
<p>JavaScript 1.2 when you do not specify <code>LANGUAGE="JavaScript1.2"</code> in the <code>&lt;SCRIPT&gt;</code> tag:
</p>
<pre class="eval">new Array([<i>arrayLength</i>])
new Array([<i>element0</i>[, <i>element1</i>[, ..., <i>elementN</i>]]])
</pre>
<p>JavaScript 1.1:
</p>
<pre class="eval">new Array([<i>arrayLength</i>])
new Array([<i>element0</i>[, <i>element1</i>[, ..., <i>elementN</i>]]]) 
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>arrayLength</code> </dt><dd> The initial length of the array. You can access this value using the length property. If the value specified is not a number, an array of length 1 is created, with the first element having the specified value. The maximum length allowed for an array is 4,294,967,295.
</dd></dl>
<dl><dt> <code>element<i>N</i></code> </dt><dd> A list of values for the array's elements. When this form is specified, the array is initialized with the specified values as its elements, and the array's length property is set to the number of arguments.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>An array is an ordered set of values associated with a single variable name.
</p><p>The following example creates an Array object with an array literal; the coffees array contains three elements and has a length of three:
</p>
<pre class="eval">coffees = ["French Roast", "Columbian", "Kona"]
</pre>
<p>You can construct a dense array of two or more elements starting with index 0 if you define initial values for all elements. A dense array is one in which each element has a value. The following code creates a dense array with three elements:
</p>
<pre class="eval">myArray = new Array("Hello", myVar, 3.14159) 
</pre>
<h4 name="Indexing_an_array"> Indexing an array </h4>
<p>You index an array by its ordinal number. For example, assume you define the following array:
</p>
<pre class="eval">myArray = new Array("Wind","Rain","Fire")
</pre>
<p>You then refer to the first element of the array as <code>myArray{{mediawiki.external(0)}}</code> and the second element of the array as <code>myArray{{mediawiki.external(1)}}</code>.
</p>
<h4 name="Specifying_a_single_parameter"> Specifying a single parameter </h4>
<p>When you specify a single numeric parameter with the <code>Array</code> constructor, you specify the initial length of the array. The following code creates an array of five elements:
</p>
<pre class="eval">billingMethod = new Array(5)
</pre>
<p>The behavior of the <code>Array</code> constructor depends on whether the single parameter is a number.
</p>
<ul><li> If the value specified is a number, the constructor converts the number to an unsigned, 32-bit integer and generates an array with the length property (size of the array) set to the integer. The array initially contains no elements, even though it might have a non-zero length.
</li></ul>
<ul><li> If the value specified is not a number, an array of length 1 is created, with the first element having the specified value.
</li></ul>
<p>The following code creates an array of length 25, then assigns values to the first three elements:
</p>
<pre class="eval">musicTypes = new Array(25)
musicTypes[0] = "R&amp;B"
musicTypes[1] = "Blues"
musicTypes[2] = "Jazz"
</pre>
<h4 name="Increasing_the_array_length_indirectly"> Increasing the array length indirectly </h4>
<p>An array's length increases if you assign a value to an element higher than the current length of the array. The following code creates an array of length 0, then assigns a value to element 99. This changes the length of the array to 100.
</p>
<pre class="eval">colors = new Array()
colors[99] = "midnightblue"
</pre>
<h4 name="Creating_an_array_using_the_result_of_a_match"> Creating an array using the result of a match </h4>
<p>The result of a match between a regular expression and a string can create an array. This array has properties and elements that provide information about the match. An array is the return value of RegExp.exec, String.match, and String.replace. To help explain these properties and elements, look at the following example and then refer to the table below:
</p>
<pre>&lt;SCRIPT LANGUAGE="JavaScript1.2"&gt;
//Match one d followed by one or more b's followed by one d
//Remember matched b's and the following d
//Ignore case

myRe=/d(b+)(d)/i;
myArray = myRe.exec("cdbBdbsbz");

&lt;/SCRIPT&gt;
</pre>
<p>The properties and elements returned from this match are as follows:
</p>
<table class="fullwidth-table">

<tbody><tr>
<td class="header">Property/Element
</td><td class="header">Description
</td><td class="header">Example
</td></tr>

<tr>
<td><code>input</code>
</td><td>A read-only property that reflects the original string against which the regular expression was matched.
</td><td>cdbBdbsbz
</td></tr>

<tr>
<td><code>index</code>
</td><td>A read-only property that is the zero-based index of the match in the string.
</td><td>1
</td></tr>

<tr>
<td><code>{{mediawiki.external(0)}}</code>
</td><td>A read-only element that specifies the last matched characters.
</td><td>dbBd
</td></tr>

<tr>
<td><code>{{mediawiki.external(1)}}, ...{{mediawiki.external('n')}}</code>
</td><td>Read-only elements that specify the parenthesized substring matches, if included in the regular expression. The number of possible parenthesized substrings is unlimited.
</td><td>{{mediawiki.external(1)}}=bB<br>{{mediawiki.external(2)}}=d
</td></tr>
</tbody></table>
<h3 name="Backward_Compatibility"> Backward Compatibility </h3>
<h4 name="JavaScript_1.2"> JavaScript 1.2 </h4>
<p>When you specify a single parameter with the Array constructor, the behavior depends on whether you specify <code>LANGUAGE="JavaScript1.2"</code> in the <code>&lt;SCRIPT&gt;</code> tag:
</p>
<ul><li> If you specify <code>LANGUAGE="JavaScript1.2"</code> in the <code>&lt;SCRIPT&gt;</code> tag, a single-element array is returned. For example, <code>new Array(5)</code> creates a one-element array with the first element being 5. A constructor with a single parameter acts in the same way as a multiple parameter constructor. You cannot specify the length property of an <code>Array</code> using a constructor with one parameter.
</li></ul>
<ul><li> If you do not specify <code>LANGUAGE="JavaScript1.2"</code> in the <code>&lt;SCRIPT&gt;</code> tag, you specify the initial length of the array as with other JavaScript versions.
</li></ul>
<h4 name="JavaScript_1.1_and_earlier"> JavaScript 1.1 and earlier </h4>
<p>When you specify a single parameter with the <code>Array</code> constructor, you specify the initial length of the array. The following code creates an array of five elements:
</p>
<pre class="eval">billingMethod = new Array(5)
</pre>
<h4 name="JavaScript_1.0"> JavaScript 1.0 </h4>
<p>You must index an array by its ordinal number; for example <code>myArray{{mediawiki.external(0)}}</code>.
</p>
<h3 name="Properties"> Properties </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/constructor">constructor</a>: Specifies the function that creates an object's prototype.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/index">index</a>: For an array created by a regular expression match, the zero-based index of the match in the string.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/input">input</a>:  For an array created by a regular expression match, reflects the original string against which the regular expression was matched.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/length">length</a>: Reflects the number of elements in an array.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/prototype">prototype</a>: Allows the addition of properties to all objects.
</p>
<h3 name="Methods"> Methods </h3>
<h4 name="About_Array_methods:_generic_methods"> About Array methods: generic methods </h4>
<p>Many methods on the JavaScript Array object are designed to be generally applied to all objects which "look like" Arrays.  That is, they can be used on any object which has a <code>length</code> property, and which can usefully be accessed using numeric property names (as with <code>array{{mediawiki.external(5)}}</code> indexing).
</p><p>TODO: give examples with Array.prototype.forEach.call, and adding the method to an object like <a href="en/Core_JavaScript_1.5_Reference/Objects/JavaArray">JavaArray</a> or <a href="en/Core_JavaScript_1.5_Reference/Objects/String">String</a>.
</p><p>Some methods, such as <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/join">join</a>, only read the <code>length</code> and numeric properties of the object they are called on.  Others, like <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/reverse">reverse</a>, require that the object's numeric properties and <code>length</code> be mutable; these methods can therefore not be called on objects like <a href="en/Core_JavaScript_1.5_Reference/Objects/String">String</a>, which does not permit its <code>length</code> property or synthesized numeric properties to be set.
</p><p>The methods that work on any Array-like object and do <b>not</b> need to alter <code>length</code> or numeric properties are:
</p>
<ul><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/concat">concat</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/every">every</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/filter">filter</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/forEach">forEach</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf">indexOf</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/join">join</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/lastIndexOf">lastIndexOf</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/map">map</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/slice">slice</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/some">some</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/toSource">toSource</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/toString">toString</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/valueOf">valueOf</a>
</li></ul>
<p>The methods that alter the <code>length</code> or numeric properties of the object they are called on are:
</p>
<ul><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/pop">pop</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/push">push</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/reverse">reverse</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/shift">shift</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/sort">sort</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/splice">splice</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/unshift">unshift</a>
</li></ul>
<h4 name="About_Array_methods:_iteration_and_mutation"> About Array methods: iteration and mutation </h4>
<p>Several Array methods take as arguments functions to be called back while processing the array (every, filter, map, some, sort).  When these methods are called, the <code>length</code> of the Array is sampled, and changes to the Array's length from within the callback are not honoured.  Other changes to the Array (setting the value of an element, or deleting an element) may affect the results of the operation if the method visits the changed element afterwards.  The specific behaviour of these methods in such cases is not always well-defined, and should not be relied upon.
</p>
<h4 name="Method_list"> Method list </h4>
<p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/concat">concat</a>: Joins two arrays and returns a new array.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/filter">filter</a>: Creates a new array with all of the elements of the current array for which the provided filtering function returns true.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/forEach">forEach</a>: Calls a function for each element in the array.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf">indexOf</a>: Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/join">join</a>: Joins all elements of an array into a string.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/lastIndexOf">lastIndexOf</a>: Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/map">map</a>: Creates a new array with the results of calling a provided function on every element in this array.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/pop">pop</a>: Removes the last element from an array and returns that element.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/push">push</a>: Adds one or more elements to the end of an array and returns the new length of the array.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/reverse">reverse</a>: Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/shift">shift</a>: Removes the first element from an array and returns that element.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/slice">slice</a>: Extracts a section of an array and returns a new array.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/sort">sort</a>: Sorts the elements of an array.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/splice">splice</a>: Adds and/or removes elements from an array.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/toSource">toSource</a>: Returns an array literal representing the specified array; you can use this value to create a new array. Overrides the Object.toSource method.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/toString">toString</a>: Returns a string representing the array and its elements. Overrides the Object.toString method.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/unshift">unshift</a>: Adds one or more elements to the front of an array and returns the new length of the array.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/valueOf">valueOf</a>: Returns the primitive value of the array.  Overrides the Object.valueOf method.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Creating_an_Array"> Example: Creating an Array </h4>
<p>The following example creates an array, <code>msgArray</code>, with a length of 0, then assigns values to <code>msgArray{{mediawiki.external(0)}}</code> and <code>msgArray{{mediawiki.external(99)}}</code>, changing the length of the array to 100.
</p>
<pre>msgArray = new Array()
msgArray[0] = "Hello"
msgArray[99] = "world"
// The following statement is true,
// because defined msgArray[99] element.
if (msgArray.length == 100)
   myVar="The length is 100."
</pre>
<h4 name="Example:_Creating_a_Two-dimensional_Array"> Example: Creating a Two-dimensional Array </h4>
<p>The following creates a two-dimensional array and assigns the results to <code>myVar</code>.
</p>
<pre>myVar="Multidimensional array test; "
a = new Array(4)
for (i=0; i &lt; 4; i++) {
   a[i] = new Array(4)
   for (j=0; j &lt; 4; j++) {
      a[i][j] = "["+i+","+j+"]"
   }
}
for (i=0; i &lt; 4; i++) {
   str = "Row "+i+":"
   for (j=0; j &lt; 4; j++) {
      str += a[i][j]
   }
   myVar += str +"; "
}
</pre>
<p>The following string is assigned to <code>myVar</code> (line breaks are used here for readability):
</p>
<pre>Multidimensional array test;
Row 0:[0,0][0,1][0,2][0,3];
Row 1:[1,0][1,1][1,2][1,3];
Row 2:[2,0][2,1][2,2][2,3];
Row 3:[3,0][3,1][3,2][3,3];
</pre>
Revert to this revision