Initialize multi-dimensional array

A

Archos

I want to initialize a multidimensional array to a determined value,
i.e. an array of dimensions [2][4] initialized to 1.

I can do:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); }
for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
[i1]=1; }}

or shorter:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}

But is there any another way to do it shorter? Without use `var a2 =
[[1,1,1,1], [1,1,1,1]]`

Thanks in advance!
 
D

Denis McMahon

I want to initialize a multidimensional array to a determined value,
i.e. an array of dimensions [2][4] initialized to 1.

var arr = new Array ( new Array(1,2,3,4), new Array(2,4,6,8), new Array
(3,6,9,12) );

Rgds

Denis McMahon
 
A

Andreas Bergmaier

Archos said:
I want to initialize a multidimensional array to a determined value

I can do:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); }
for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
[i1]=1; }}

or shorter:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}

But is there any another way to do it shorter? Without use `var a2 =
[[1,1,1,1], [1,1,1,1]]`

What do you mean with "shorter"? Less code? Faster execution? If you
don't want to use array literals, there will always be two loops:

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

There might be tricky expressions with less code, but you don't really
want them.

Bergi
 
A

Archos

Archos said:
I want to initialize a multidimensional array to a determined value
I can do:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); }
for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
[i1]=1; }}
or shorter:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}
But is there any another way to do it shorter? Without use `var a2 =
[[1,1,1,1], [1,1,1,1]]`

What do you mean with "shorter"? Less code? Faster execution? If you
don't want to use array literals, there will always be two loops:

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

There might be tricky expressions with less code, but you don't really
want them.

  Bergi


I mean less code, and using simple '[]' is saved some space, but is it
implemented in the main browsers or I could have any problem by its
use?

Which has a faster execution (array literal or '[]')?
 
R

Richard Cornford

On Jan 25, 10:48 am, Andreas Bergmaier wrote:

I mean less code,

Why, what is the problem with having more code?
and using simple '[]' is saved some space, but is it
implemented in the main browsers or I could have any problem
by its use?

Array literals where formalised in ECMA 262 3rd Ed at the end
of 1999, so has been around for long enough to be universally
implemented in web browser's javascript engines.
Which has a faster execution (array literal or '[]')?

[] is an array literal; a literal declaration for an empty array.

Richard.
 
M

Michael Haufe (TNO)

I want to initialize a multidimensional array to a determined value,
i.e. an array of dimensions [2][4] initialized to 1.

I can do:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); }
for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
[i1]=1; }}

or shorter:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}

But is there any another way to do it shorter? Without use `var a2 =
[[1,1,1,1], [1,1,1,1]]`

Thanks in advance!

Use an Array Comprehension.
 
T

Thomas 'PointedEars' Lahn

Andreas said:
Archos said:
I want to initialize a multidimensional array to a determined value

I can do:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); }
for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
[i1]=1; }}

or shorter:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}

But is there any another way to do it shorter? Without use `var a2 =
[[1,1,1,1], [1,1,1,1]]`

What do you mean with "shorter"? Less code? Faster execution? If you
don't want to use array literals, there will always be two loops:
No.

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

^^ ^^
JFYI: You are using two Array literals here, of course.
There might be tricky expressions with less code, but you don't really
want them.

A matter of opinion. Surely

var a = [];
a.length = 5;
a = a.join("1").split("").map(function (el) { return +el; });
a = [a, a.slice()];

is a more elegant solution to this problem.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <81f35d2e-80d0-45aa-965a-c0093cbf4fbf@b2
3g2000yqn.googlegroups.com>, Tue, 24 Jan 2012 15:09:00, Archos
I want to initialize a multidimensional array to a determined value,
i.e. an array of dimensions [2][4] initialized to 1.

I can do:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); }
for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
[i1]=1; }}

or shorter:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}

But is there any another way to do it shorter? Without use `var a2 =
[[1,1,1,1], [1,1,1,1]]`

Slightly. Use [] instead of new Array(n). A while loop counting down
is probably shorter & faster than a for loop counting up, and you can
combine the decrement and the "was that zero?" test.

var X, j, k, A = []
j = 2 ; do { A[--j] = X = []
k = 4 ; do { X[--k] = 1 }
while (k) }
while (j) ;

if your '4' is both small and constant, and you don't mind setting to
"1" rather than 1, this is shorter

var X, j, A = []
j = 2 ; do { A[--j] = X = "1111".split("") } while (j) ;

and if it is variable but under about 54, and ditto,

var X, j, A = []
j = 2, k = 99
do { A[--j] = X = (Math.pow(2, k)-1).toString(2).split("") } while (j) ;

Inadequately tested.
 
D

Dr J R Stockton

In comp.lang.javascript message <3g81i7l6qma04ij6jsef0hjprjochsvq4q@4ax.
com>, Thu, 26 Jan 2012 11:56:11, Ross McKay <au.org.zeta.at.rosko@invali
d.invalid> posted:
[...] A while loop counting down
is probably shorter & faster than a for loop counting up, [...]

No.

http://jsperf.com/caching-array-length/4


I prefer to rely on my own tests, done with a large enough loop for time
to begin to matter.

var A, J, K_ = 777 // increase v put code in those
D0_ = new Date()
Q_ = K_ ;
D1_ = new Date()
Q_ = K_ ; while (Q_--) { }
D2_ = new Date()
Q_ = K_ ; while (Q_--) { A = [] ; J = K_ ; while (--J) A[J] = J }
D3_ = new Date()
Q_ = K_ ; while (Q_--) { A = [] ; for (J = 0 ; J < K_ ; J++) A[J] = J }
D4_ = new Date()
Q_ = [D1_-D0_, D2_-D1_, D3_-D2_, D4_-D3_] // times // Demo 6

The while loop is clearly shorter. And I find it faster in three
browsers out of five.
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Andreas said:
var a=[];for(var i=0;i<2;i++){a=[];for(var j=0;j<4;j++)a[j]=1;}

^^ ^^
JFYI: You are using two Array literals here, of course.
There might be tricky expressions with less code, but you don't really
want them.

A matter of opinion. Surely

var a = [];
a.length = 5;
a = a.join("1").split("").map(function (el) { return +el; });
a = [a, a.slice()];

is a more elegant solution to this problem.


It should be noted that this approach can also be used with multi-digit
numbers and any other value:

var a = [];
a.length = 5;
a = a.join(",12").split(",").map(function (el) { return +el; });
a.shift();
a = [a, a.slice()];

However more elegant, the map() method is not backwards compatible (so it
requires comparably expensive emulation) and it is expensive itself because
the function needs to be called multiple times. Also, the need for
additional shift() (or pop(), if you use "12," instead) with multi-character
values reduces its elegance.

So a combination of the classical looping approach and the more elegant
slicing approach (which performs the element assignment natively, therefore
should be cheaper than an explicit loop) appears to be the best solution:

function getFilledMatrix(rows, columns, fill)
{
var a = [];

var tmp = [];
tmp.length = columns;

for (var j = 0; j < columns; ++j)
{
tmp[j] = fill || 0;
}

for (var i = 0; i < rows; ++i)
{
if (i == 0)
{
a.push(tmp);
}
else
{
a.push(tmp.slice());
}
}

return a;
}


PointedEars
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top