Debugging multi-array

Discussion in 'Java' started by lielar, Mar 9, 2008.

  1. lielar

    lielar Guest

    Hi

    I have the following multi-array, I don't know how to follow it.

    String [][][] arr = {
    {{}, null}, {{"1", "2"}, {"1", null, "3"}},
    {}, {{"1", null}}

    };

    I have problems knowing what is the first, second, third objects. And
    it being an array of an array of an array(3x), if one value is not
    specified, how can you tell?
    lielar, Mar 9, 2008
    #1
    1. Advertising

  2. lielar

    Chris Guest

    lielar wrote:
    > Hi
    >
    > I have the following multi-array, I don't know how to follow it.
    >
    > String [][][] arr = {
    > {{}, null}, {{"1", "2"}, {"1", null, "3"}},
    > {}, {{"1", null}}
    >
    > };
    >
    > I have problems knowing what is the first, second, third objects. And
    > it being an array of an array of an array(3x), if one value is not
    > specified, how can you tell?
    >

    The first index refers to the outer array, the next to the next inner
    array, etc.

    If you don't know in advance how long each array is, then you must check
    lengths before accessing an element.

    For example, to access arr[1][2][3], do this:

    if (arr.length >= 2) {
    String [][] arr0 = arr[1];
    if (arr0.length >= 3) {
    String [] arr1 = arr0[2];
    if (arr1.length >= 4) {
    String arr2 = arr1[3];
    // use the value here
    }
    }
    }

    This is ugly. I'm sure you can find a more efficient way to do it.
    Chris, Mar 9, 2008
    #2
    1. Advertising

  3. lielar

    Lew Guest

    Chris wrote:
    > If you don't know in advance how long each array is, then you must check
    > lengths before accessing an element.


    Worse, you have to check for null, too.

    > For example, to access arr[1][2][3], do this:
    >

    if ( arr != null && arr.length >= 2) {
    > String [][] arr0 = arr[1];

    if ( arr0 != null && arr0.length >= 3) {
    > String [] arr1 = arr0[2];

    if ( arr1 != null && arr1.length >= 4) {
    > String arr2 = arr1[3];
    > // use the value here

    // checking for null
    > }
    > }
    > }
    >
    > This is ugly. I'm sure you can find a more efficient way to do it.



    --
    Lew
    Lew, Mar 9, 2008
    #3
    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. John Grandy
    Replies:
    0
    Views:
    609
    John Grandy
    Sep 13, 2005
  2. Dave
    Replies:
    3
    Views:
    390
    Kevin Goodsell
    Apr 19, 2004
  3. Replies:
    38
    Views:
    1,273
    Dennis Lee Bieber
    Feb 15, 2005
  4. ian douglas
    Replies:
    2
    Views:
    980
    Randy Howard
    Jul 30, 2004
  5. akineko
    Replies:
    3
    Views:
    2,623
    Jesse Noller
    Jan 29, 2009
Loading...

Share This Page