shivam.k
shivam.k

Reputation: 13

Unable to understand what Javascript new Array( new Array(5,4,3,2,1,0),new Array())

Please tell me what this code is doing ,is it creating multi dimension array(which i think it is not) ?

code snippet..

 var hanoi_peg = new Array(
   new Array(  5,  4,  3,  2,  1,  0 ),
   new Array( -1, -1, -1, -1, -1, -1 ),
   new Array( -1, -1, -1, -1, -1, -1 )
 );

thanks..

Upvotes: 0

Views: 81

Answers (4)

Suren Srapyan
Suren Srapyan

Reputation: 68665

Log it into your console and you will see that it creates an array of arrays and it serves for the same purpose.

var hanoi_peg = new Array(
   new Array(  5,  4,  3,  2,  1,  0 ),
   new Array( -1, -1, -1, -1, -1, -1 ),
   new Array( -1, -1, -1, -1, -1, -1 )
 );

console.log(hanoi_peg);

To access each item you must use double [] operators like this. For accessing the first item of the first array you need to call

console.log(hanoi_peg[0][0]);

It is same as

var firstArray = hanoi_peg[0];
var firstItemOfFirstArray = firstArray[0]; // hanoi_peg[0][0]

Upvotes: 2

Mamdouh Saeed
Mamdouh Saeed

Reputation: 2324

your code is like this

 //define array
            var hanoi_peg = [];
            //define 1st element and its array also
            hanoi_peg[0] = [5,  4,  3,  2,  1,  0];
            //define 2nd element and its array also
            hanoi_peg[1] = [-1, -1, -1, -1, -1, -1];
            //define 3rd element and its array also
            hanoi_peg[2] = [-1, -1, -1, -1, -1, -1];
            
        console.log("full array: "+JSON.stringify(hanoi_peg));
        /*
        [
         [5,  4,  3,  2,  1,  0],
         [-1, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1]
        ]
        */
        console.log("first element of array: ",hanoi_peg[0]);//[5,  4,  3,  2,  1,  0];
        console.log("first element of inner array: "+hanoi_peg[0][0]);//5

Upvotes: 0

Rajesh
Rajesh

Reputation: 24945

Its creating an array of arrays

new Array() will return new array object. Array has 2 overrided constructors.

new Array(length)

This will return an array of specified length with all elements set as undefined. Note, this will only happen if length is integer. If you try new Array('str') this will return ['str']. If you do new Array(2.5), it will throw error as argument is numeric but not integer.

new Array(element list)

This will create an array with mentioned elements.

So when you do new Array(new Array(1,2,3)), new Array(1,2,3) will execute first returning [1,2,3] and then new Array([1,2,3]) returning [[1,2,3]]

var hanoi_peg = new Array(
   new Array(  5,  4,  3,  2,  1,  0 ),
   new Array( -1, -1, -1, -1, -1, -1 ),
   new Array( -1, -1, -1, -1, -1, -1 )
 );

console.log(hanoi_peg);
console.log(new Array('str'))

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074949

It's creating an array of arrays. That's not quite the same as a multi-dimensional array, but it serves a similar purpose.

You can get the value 3, for instance, by doing this:

console.log(hanoi_peg[0][2]);

Normally you'd use array literal notation instead:

var hanoi_peg = [
   [  5,  4,  3,  2,  1,  0 ],
   [ -1, -1, -1, -1, -1, -1 ],
   [ -1, -1, -1, -1, -1, -1 ]
];

The difference between an array of arrays and a multi-dimensional array is that an array of arrays isn't stored in a single contiguous block of memory, and as a result can be jagged; the arrays in the second level don't all have to have the same number of elements. For instance, this is perfectly valid:

var a = [
    [1, 2, 3, 4, 5],
    [1, 2, 3],
    [1, 2, 3, 4, 5, 6]
];

In a true multi-dimensional array, there's one contiguous block of memory (in effect, one big single-dimensional array) and the compiler/runtime just does math on the indexes to find the right slot. For instance, a true 5x5 multi-dimensional array would be a single contiguous set of 25 slots, and when you asked for the entry at [2][4], the compiler/runtime would turn that into [(2*5)+4] and return the contents of slot index 14.

But with an array of arrays, [2][4] is two different operations: First, get the entry at [2], which is a reference to an array; then get the entry at [4] from that array.

Upvotes: 3

Related Questions