简体   繁体   English

Java中的多维(数组数组)。 它们是行还是列数组?

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

I have a 2D array in Java that is 我在Java中有一个2D数组

 [[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: 因此,上面的数组将显示为A:

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

or, B: 或者,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: 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). 如果需要有意义的行/列关系,则应将其包装在Table类中,或使用其他人为您创建的类(Guava想到了)。

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

It would look like B . 看起来像B。

int[2][3] means that there are 2 arrays of length 3. int [2] [3]表示有2个长度为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 . 答案是B。

Remember that there is no multidimensional array in Java, it is actually an array of array , just like your array : 请记住,Java中没有多维数组,它实际上是array的数组 ,就像您的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 B,按以下代码检查

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]);
            }   
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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