# Create a multi-dimensional array

Discussion in 'Javascript' started by Archos, Nov 23, 2011.

1. ### ArchosGuest

To create a two dimensional array I use:

var a4 = new Array(3); for (i=0; i<3; i++) a4=new Array(5);

In pseudoce: a4 = [3][5]

Then, to create a 3 dim. array, I tried this one:

var a5 = new Array(2); for (i=0; i<2; i++) a5=new Array(2); for
(i=0; i<2; i++) a5=new Array(2);

a5 = [2][2][2]

but the firebug console shows that it has not been defined the las
dimension
undefined

Archos, Nov 23, 2011

2. ### Jukka K. KorpelaGuest

The value of a4 is an array, where the elements are arrays. Calling it a
two-dimensional array probably confuses rather than helps. The notation
[3][5] is not meaningful as such.

I don't see what you regard as three-dimensional here, and I don't
understand why you have the same 'for' loop twice.
Naturally, because no value has been assigned to it. You have created an
array with two elements, each of which is an array of two elements, each
of which is initially undefined. Try setting e.g.
a5[1] = ['Hello world', 42]
or
a5[1][0] = 'foobar'
and you'll see that a5 works fine as a simulation of a two-dimensional
array.

Jukka K. Korpela, Nov 23, 2011

3. ### Erwin MollerGuest

Hi Archos,

All on 1 line, and no {} to make your intentions clear.

Did you mean to do this?
var a5 = new Array(2);
for (i=0; i<2; i++){
a5=new Array(2);
for (i=0; i<2; i++){
a5=new Array(2);
}
}

That would be wrong of course.
2 times i, and the second loop also does something strange with the
indexes of a5.

What you want, however, is probably something like this:

var a5 = new Array(2);
for (var i=0; i<2; i++){
a5=new Array(2);
for (var j=0; j<2; j++){
a5[j]=new Array(2);
}
}

That uses i and j to make things clearer and uses "var".
Now you can say:
a5[1][1][0] = "whatever";

Regards,
Erwin Moller

Erwin Moller, Nov 23, 2011
4. ### ArchosGuest

Could be used "let" instead of "var"? since it's for local scope.

Thanks!

Archos, Nov 23, 2011
5. ### John G HarrisGuest

It's more than a 'simulation'. If it's a structure that correctly
implements the job of a two-dimensional array then it *is* a two
dimensional array.

Just because programs are not built into the language doesn't mean that
programs are mere 'simulations'.

John

John G Harris, Nov 23, 2011
6. ### Denis McMahonGuest

Yes, because all you've done is define a5 twice. I think that what you
want to do is define a5[?] as an array?

Try (untested):

var a5 = new Array(2);
for (var i=0; i<2; i++)
{
a5=new Array(2);
for (var j=0; j<2; j++) a5[j]=new Array(2);
}

Rgds

Denis McMahon

Denis McMahon, Nov 23, 2011
7. ### Thomas 'PointedEars' LahnGuest

Using arguments for the Array constructor is pointless here and error-prone.
For that matter, using the Array constructor is unnecessary and potentially
error-prone. Use the Array initializer instead; it is most certainly
ubiquitous by now [1]:

var a5 = [];
for (var i = 0; i < 2; ++i)
{
a5 = [];
for (var j = 0; j < 2; ++j)
{
a5[j] = [];
}
}

Note that you can save memory if you do not create all potentially needed
Array instances, but you have to pay for that with a bit of runtime (tests
before access whether there already is an Array instance reference
assigned).

PointedEars
___________
[1] <http://PointedEars.de/es-matrix/#!>

Thomas 'PointedEars' Lahn, Nov 23, 2011
8. ### Dr J R StocktonGuest

It will help if the reader is accustomed to languages which support
multi-dimensional arrays, and wants in JavaScript the effect of a two-
dimensional array. Those languages include Algol (IIRC), Pascal/Delphi,
and English.
The author meant to say that it is pseudocode. It's certainly
meaningful in some dialects of pseudocode.

Provided that one recalls that a5 must be an array (maybe empty) before
the first of those assignments, and that a5[1] must be an array (maybe
empty) before the second of those assignments.

Dr J R Stockton, Nov 24, 2011
9. ### Jukka K. KorpelaGuest

Sounds like confusion. Do you really wish to promote such ideas?
Nonsense does not become any more meaningful by someone's calling it
"pseudocode".
Are you trying to make a point, or just noise?

Jukka K. Korpela, Nov 24, 2011
10. ### Gene WirchenkoGuest

At least two of us understood it just fine. Check your language
calibration?

[snip]

Sincerely,

Gene Wirchenko

Gene Wirchenko, Nov 25, 2011