Create a multi-dimensional array

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

  1. Archos

    Archos Guest

    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

    >>> console.log(a5[1][0])

    undefined
     
    Archos, Nov 23, 2011
    #1
    1. Advertising

  2. 2011-11-23 13:04, Archos wrote:

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

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
     
    Jukka K. Korpela, Nov 23, 2011
    #2
    1. Advertising

  3. Archos

    Erwin Moller Guest

    On 11/23/2011 12:04 PM, Archos wrote:
    > 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



    --
    "That which can be asserted without evidence, can be dismissed without
    evidence."
    -- Christopher Hitchens
     
    Erwin Moller, Nov 23, 2011
    #3
  4. Archos

    Archos Guest

    On Nov 23, 11:46 am, Erwin Moller
    <> wrote:
    > 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!
     
    Archos, Nov 23, 2011
    #4
  5. On Wed, 23 Nov 2011 at 13:41:28, in comp.lang.javascript, Jukka K.
    Korpela wrote:

    <snip>
    >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
    --
    John Harris
     
    John G Harris, Nov 23, 2011
    #5
  6. On Wed, 23 Nov 2011 03:04:49 -0800, Archos wrote:

    > 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
     
    Denis McMahon, Nov 23, 2011
    #6
  7. Denis McMahon wrote:

    > 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/#!>
    --
    Danny Goodman's books are out of date and teach practices that are
    positively harmful for cross-browser scripting.
    -- Richard Cornford, cljs, <cife6q$253$1$> (2004)
     
    Thomas 'PointedEars' Lahn, Nov 23, 2011
    #7
  8. In comp.lang.javascript message <jaim56$151$>, Wed, 23
    Nov 2011 13:41:28, Jukka K. Korpela <> posted:

    >2011-11-23 13:04, Archos wrote:
    >
    >> 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.

    --
    (c) John Stockton, nr London UK ?@merlyn.demon.co.uk IE8 FF8 Op11 Sf5 Cr15
    news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
    <http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Nov 24, 2011
    #8
  9. 2011-11-24 20:42, Dr J R Stockton wrote:

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


    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?

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
     
    Jukka K. Korpela, Nov 24, 2011
    #9
  10. On Fri, 25 Nov 2011 01:53:58 +0200, "Jukka K. Korpela"
    <> wrote:

    >2011-11-24 20:42, Dr J R Stockton wrote:
    >
    >>> 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,

    >
    >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
     
    Gene Wirchenko, Nov 25, 2011
    #10
    1. Advertising

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,172
  2. Venkat
    Replies:
    4
    Views:
    994
    Venkat
    Dec 5, 2003
  3. Anthony Martinez

    Helper to create multi-dimensional arrays

    Anthony Martinez, Jun 24, 2007, in forum: Ruby
    Replies:
    20
    Views:
    300
  4. Wirianto Djunaidi
    Replies:
    2
    Views:
    214
    Wirianto Djunaidi
    Apr 29, 2008
  5. SM
    Replies:
    10
    Views:
    238
Loading...

Share This Page