Initialize multi-dimensional array

Discussion in 'Javascript' started by Archos, Jan 24, 2012.

  1. Archos

    Archos Guest

    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!
     
    Archos, Jan 24, 2012
    #1
    1. Advertisements

  2. On Tue, 24 Jan 2012 15:09:00 -0800, Archos wrote:

    > 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
     
    Denis McMahon, Jan 25, 2012
    #2
    1. Advertisements

  3. Archos schrieb:
    > 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
     
    Andreas Bergmaier, Jan 25, 2012
    #3
  4. Archos

    Archos Guest

    On Jan 25, 10:48 am, Andreas Bergmaier <> wrote:
    > Archos schrieb:
    >
    > > 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 '[]')?
     
    Archos, Jan 25, 2012
    #4
  5. On Jan 25, 12:37 pm, Archos wrote:
    > On Jan 25, 10:48 am, Andreas Bergmaier wrote:

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

    >
    > 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.
     
    Richard Cornford, Jan 25, 2012
    #5
  6. On Jan 24, 5:09 pm, Archos <> wrote:
    > 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.
     
    Michael Haufe (TNO), Jan 25, 2012
    #6
  7. Andreas Bergmaier wrote:

    > Archos schrieb:
    >> 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
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Jan 25, 2012
    #7
  8. In comp.lang.javascript message <81f35d2e-80d0-45aa-965a-c0093cbf4fbf@b2
    3g2000yqn.googlegroups.com>, Tue, 24 Jan 2012 15:09:00, Archos
    <> posted:

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

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
    Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Jan 25, 2012
    #8
  9. Archos

    Ross McKay Guest

    On Wed, 25 Jan 2012 19:33:41 +0000, Dr J R Stockton wrote:

    >[...] A while loop counting down
    >is probably shorter & faster than a for loop counting up, [...]


    No.

    http://jsperf.com/caching-array-length/4
    --
    Ross McKay, Toronto, NSW Australia
    "You can't control what you can't measure" - Tom DeMarco
     
    Ross McKay, Jan 26, 2012
    #9
  10. 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:

    >On Wed, 25 Jan 2012 19:33:41 +0000, Dr J R Stockton wrote:
    >
    >>[...] 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.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
     
    Dr J R Stockton, Jan 27, 2012
    #10
  11. Thomas 'PointedEars' Lahn wrote:

    > Andreas Bergmaier wrote:
    >> 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
    --
    When all you know is jQuery, every problem looks $(olvable).
     
    Thomas 'PointedEars' Lahn, Jan 28, 2012
    #11
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kk_oop
    Replies:
    1
    Views:
    2,337
  2. Replies:
    7
    Views:
    1,068
    Ryan Stewart
    Feb 20, 2005
  3. John Harrison
    Replies:
    2
    Views:
    928
    John Harrison
    Jul 14, 2003
  4. Tom
    Replies:
    9
    Views:
    847
    Victor Bazarov
    Oct 19, 2003
  5. Venkat
    Replies:
    4
    Views:
    1,321
    Venkat
    Dec 5, 2003
  6. sugaray
    Replies:
    1
    Views:
    7,063
    Rob Williscroft
    Dec 25, 2003
  7. Tony Johansson

    Initialize a two-dimensional array

    Tony Johansson, Aug 29, 2003, in forum: C Programming
    Replies:
    0
    Views:
    464
    Tony Johansson
    Aug 29, 2003
  8. Wirianto Djunaidi
    Replies:
    2
    Views:
    410
    Wirianto Djunaidi
    Apr 29, 2008
Loading...