简体   繁体   中英

Multi-dimensional (array of arrays) in Java. Are they row or column arrays?

I have a 2D array in Java that is

 [[1 1 1 1 1] [2 2 2 2 2] [3 3 3 3 3] [4 4 4 4 4]]

And I was wondering how this would appear if drawn out on paper, ie what does each individual array translate to.

So would the above array appear as, A:

 1 2 3 4 
 1 2 3 4 
 1 2 3 4 
 1 2 3 4
 1 2 3 4 

or, B:

1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 

I know this may seem a basic question but I can't find an answer and it is obviously fundamental to my programme.

C:

1 1 1 1 1   2 2 2 2 2   3 3 3 3 3   4 4 4 4 4

Whether they are rows or columns is meaningless and arbitrary in this case, and depends entirely on how you interpret the data. It is simply an array of arrays, and whether you decide it's column-first, or row-first is entirely up to you. Just make sure you always do it the same way.

If you want a meaningful row/column relationship, you should wrap it in a Table class, or use one that someone else has made for you (Guava comes to mind).

这取决于您如何感知和使用它。

It would look like B .

int[2][3] means that there are 2 arrays of length 3.

so it would be like

array1 : x x x
array2 : x x x

This is how they are located in memory, you can print them in various ways of course.

The answer is B .

Remember that there is no multidimensional array in Java, it is actually an array of array , just like your array :

[
   [1 1 1 1 1],
   [2 2 2 2 2],
   [3 3 3 3 3],
   [4 4 4 4 4],
]

B, check it by code below

int[][] a = new int[][]{{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3},{4,4,4,4,4}};
        for(int i=0;i<4;i++){
            for(int j=0;j<5;j++){
                System.out.println(a[i][j]);
            }   
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM