开发者

How do I reference multi-dimensional arrays?

开发者 https://www.devze.com 2022-12-17 05:05 出处:网络
I\'m trying to understandmulti-dimensional arrays in Java, but I think it applies to any language.Please consider this snippet I found on the web:

I'm trying to understand multi-dimensional arrays in Java, but I think it applies to any language. Please consider this snippet I found on the web:

          //************************************
            //     static array initialization
            //************************************
    byte[][] smallArray = { { 10, 11, 12, 13 },
    { 20, 21, 22, 23 },
    { 30, 31, 32, 33 },
    { 40, 41, 42, 43 }, };

    // Display the array element at row 2, column 3
    System.out.println(smallArray[1][2]); 
// Value is 21

Why is it 21? I thought it would be 22.

I looked this up because I am trying to figure out what length means in this context:

public class Pieces {
    int thePieces[][][];
    public Pieces(){

    }
    public int[][][] createPieces(){

        /* pieces in order below
                **** [0]

                **
                **

                *
                ***开发者_运维技巧*

                   *
                ****

                ***
                 *

                **
                 **

                 **
                ** [6]
         *
         *
         *
         */
       int myPieces [][][] =
       {
           {  //square
               {0,0},
               {1,0},
               {1,1},
               {0,1}

           },
           { //T
               {-1,0},
               {0,0},
               {0,1},
               {1,0}
           }

       };

       thePieces = myPieces;

    return thePieces;
    }

    public void printPieces(){

        System.out.println(thePieces[0][0].length);
    }

}

I am trying to learn java by creating a tetris clone. What I don't understand is why length is required to use [][]. It was the only way I could get to work. I tried thePieces[0][0][0].length but got an error that I did not understand?

Why don't I have to put [][][] to get the reference? I saw online where the first "level" of the array was referenced myArray.length instead of myArray[].length. What then am I saying with myArray[][].length (it seems 2nd,3rd levels but I'm not getting what I expect)?

I ran my piece code and got 2. I have no idea why it is two. Is it because I have two sets at the third level? I expected to get four because of the four sets of points.

Thank you for your helping me understand.

EDIT: I figured out why it is two. It is giving me my two points in the innermost set. If I wanted four I would use thePieces[0].length. But what about the first example? Is it a typo?


Why is it 21? I thought it would be 22.

22 is correct -- I ran the code to verify it.

The indexes refer to the array elements, from the outer ones to the inner ones. So,

myPieces[0] is { {0,0}, {1,0}, {1,1}, {0,1} } (an array containing arrays), myPieces[0][0] is {0,0} (an array containing ints), and myPieces[0][0][0] is 0 (an int).


Why is it 21? I thought it would be 22.

The example is wrong. It will be 22.

I tried thePieces[0][0][0].length but got an error that I did not understand?

This requires a little explanation. When you say:

int[][][] thePieces = { ... }

You are declaring a variable called thePieces to be an array of arrays of arrays of ints. That sounds funny. Anyway, thePieces.length is valid, because thePieces is an array of some sort. When you dereference the array, like so:

thePieces[index]

You get an array of arrays of ints, which you can call length on, because it is an array of something. SO:

thePieces[index0][index1]

is an array of ints, which you can call length on (I hope you see a pattern by now).

thePieces[index0][index1][index2]

Gives you a plain-old int, which does not have a length property.

I ran my piece code and got 2. I have no idea why it is two. Is it because I have two sets at the third level? I expected to get four because of the four sets of points.

You call:

System.out.println(thePieces[0][0].length);

Which prints 2 because thePieces[0] points to this array:

{  //square
    {0,0},
    {1,0},
    {1,1},
    {0,1}
}

which would return 4 (if you called thePieces[0].length). However, thePieces[0][0] points to:

{0, 0}

which has length of 2.


As the other posters have noted, you are correct in thinking that it should be output '22'.

As a side note, unless you have a specific reason not to you should consider using one of the Java Collections as opposed to an array. Perhaps a Vector of Vectors (ie: Vector<Vector<Integer>>). Not that is incorrect to use an array, however I personally find it much easier to work with Collections than Arrays.

0

精彩评论

暂无评论...
验证码 换一张
取 消