dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: Accessing values in multidimensional array

  1. #1
    Join Date
    Aug 2016
    Posts
    24

    Accessing values in multidimensional array

    I have a two-dimensional array of Boolean T/F values;
    let's call it "bits[x, y]", with x numbering the columns and y numbering not "rows", but rather items in each column.
    I declare it as follows:

    xMax = 20;
    yMax = 16;
    bits = new Array(xMax, yMax);


    Problem: When I use "alert" to check on any values, regardless of what column I specify, it always gives me only values of the last column. For example, say there are four populated columns (#0-#3) and I want to get the first five values (0-4) in the second column (col. #1):

    alert(bits[1,0] + " " + bits[1,1] + " " + bits[1,2] + " " + bits[1,3] + " " + bits[1,4]);

    Instead, it gives me the values for col. #3, the last column.
    What am I doing wrong?

  2. #2
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,913

    Question

    Can you expand upon your question further?

    You initialize the size of the array, but you are not putting any values into it,
    therefore the entire array contents are undefined. What are the values you are trying
    to use or are being displayed by the alert?

    You talk about 4 populated columns then skip to 5 values in second column.
    Can you give some examples of what this means by using some specific values you enter and/or expect?

    You use 'bits' as your array name. Do you truly want to use bits (bytes) as your element contents or is just the name you have chosen.

    Some clarification is needed for me to be able to help.

  3. #3
    Join Date
    Aug 2016
    Posts
    24
    Thanks for your reply!
    ........Column
    ........0 1 2 3
    Item 0: F T F T
    .....1: T F T F
    .....2: F F F T
    .....3: T F F T
    .....4: F F T F
    .....5: F F T T

    Hopefully my use of dots has got the Ts and Fs lined up with their column numbers.
    The values, true or false, can't be initialized because they depend on a word of input from the user.
    When I verify, say, column 1, I want to see "true false false false false false",
    but instead I get "true false true true false true", which you can see is the contents of column 3.
    It's always the last column.
    If I use a loop to verify all four columns, it gives me the last column four times.
    The array is called "bits" because it is the binary representation of the user's word.

  4. #4
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,913
    Do you have any JS attempts at this time?

  5. #5
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    563
    I don't think I have seen an array defined as more than one dimension. It's more like:
    Code:
    var bits = new Array();
    for (x = 0; x < xMax; x++) {
      bits[x] = new Array();
      for (y = 0; y < yMax;y++) {
        bits[x][y] = whatever;
      }
    }
    The 2nd dimension doesn't exist until the 1 dim. occurrence is defined as an array, then the second subscript is needed to create that occurrence and give it a value.

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,385
    Quote Originally Posted by Leezinho View Post
    I have a two-dimensional array of Boolean T/F values;
    let's call it "bits[x, y]", with x numbering the columns and y numbering not "rows", but rather items in each column.
    I declare it as follows:

    xMax = 20;
    yMax = 16;
    bits = new Array(xMax, yMax);


    Problem: When I use "alert" to check on any values, regardless of what column I specify, it always gives me only values of the last column. For example, say there are four populated columns (#0-#3) and I want to get the first five values (0-4) in the second column (col. #1):

    alert(bits[1,0] + " " + bits[1,1] + " " + bits[1,2] + " " + bits[1,3] + " " + bits[1,4]);

    Instead, it gives me the values for col. #3, the last column.
    What am I doing wrong?
    When you bits = new Array(xMax, yMax); you created an array with two data elements, not a multi-dimensional array.

    To make a multi-dimensional array in javascript, you need to make the array base elements, example, make an array with 10 by 10 elements
    Code:
    myArray = new Array( 9 );
    for(l=0; l<9; l++) myArray[l] = new Array(9);
    you will have an array that is 10 x 10 because all JavaScript arrays start at zero.

    in your case, you want an array structure that is... 6 x 4, so you would need
    Code:
    myArray = new Array( 9 );
    for(l=0; l<6; l++) myArray[l] = new Array(4);
    Then you would need to put in the data as needed.
    You have an alternative method, if your array structure is as you lay it out... you can simply do this
    Code:
    var T=1, F=0;
    myArray = [
        [F,T,F,T],
        [T,F,T,F],
        [T,F,F,T],
        [T,F,F,T],
        [F,F,T,F],
        [F,F,T,T]
    ];
    will produce the desired array structure. So myArray[0][0] will be F

    So the next question is, what are your intentions? Sometimes a multidimensional array is not the best way to go and an Array of Objects is sometimes better or sometimes an Object with Arrays, depending on what you are doing.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  7. #7
    Join Date
    Aug 2016
    Posts
    24
    Thanks for your responses. ArrayName[x][y], an array of arrays, not ArrayName[x, y]. Of course. Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center