Create a multi-dimensional array

A

Archos

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
 
J

Jukka K. Korpela

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]


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.
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);


I don't see what you regard as three-dimensional here, and I don't
understand why you have the same 'for' loop twice.
console.log(a5[1][0])
undefined

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.
 
E

Erwin Moller

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);


Hi Archos,

I find it difficult to read your Javascript coding style.
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

a5 = [2][2][2]

but the firebug console shows that it has not been defined the las
dimension
console.log(a5[1][0])
undefined
 
A

Archos

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".

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

Thanks!
 
J

John G Harris

and you'll see that a5 works fine as a simulation of a two-dimensional
array.

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
 
D

Denis McMahon

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]


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
 
T

Thomas 'PointedEars' Lahn

Denis said:
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);
}


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/#!>
 
D

Dr J R Stockton

Wed said:
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]


The value of a4 is an array, where the elements are arrays. Calling it
a two-dimensional array probably confuses rather than helps.


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 notation [3][5] is not meaningful as such.

The author meant to say that it is pseudocode. It's certainly
meaningful in some dialects of pseudocode.

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.

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.
 
J

Jukka K. Korpela

2011-11-24 20:42 said:
It will help if the reader is accustomed to languages which support
multi-dimensional arrays,

Sounds like confusion. Do you really wish to promote such ideas?
The notation [3][5] is not meaningful as such.

The author meant to say that it is pseudocode. It's certainly
meaningful in some dialects of pseudocode.

Nonsense does not become any more meaningful by someone's calling it
"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.

Are you trying to make a point, or just noise?
 
G

Gene Wirchenko

2011-11-24 20:42 said:
It will help if the reader is accustomed to languages which support
multi-dimensional arrays,

Sounds like confusion. Do you really wish to promote such ideas?
The notation [3][5] is not meaningful as such.

The author meant to say that it is pseudocode. It's certainly
meaningful in some dialects of pseudocode.

Nonsense does not become any more meaningful by someone's calling it
"pseudocode".

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

[snip]

Sincerely,

Gene Wirchenko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top