简体   繁体   English

3D java阵列打印,另一种方式

[英]3D java array print, another way

Hello fellow programmers! 各位程序员大家好!

I will post here the whole code i have. 我将在这里发布我的全部代码。 What I am trying to get is a full list of what's inside the array. 我想要得到的是数组内部的完整列表。 I figured out how to do it with for loop and .length. 我想出了如何用for循环和.length来做到这一点。

What I am asking here is: Is there any other way to print whole array? 我在这里问的是:有没有其他方法可以打印整个阵列?

Code starts here: 代码从这里开始:

double[][][] skuskaTroj = new double[][][] {
        {
                { 12.44, 525.38, -6.28, 2448.32, 632.04 }, // [0][][]
                { -378.05, 48.14, 634.18, 762.48, 83.02 },
                { 64.92, -7.44, 86.74, -534.60, 386.73 } 
        },{
                { 48.02, 120.44, 38.62, 526.82, 1704.62 }, // [1][][]
                { 56.85, 105.48, 363.31, 172.62, 128.48 },
                { 906.68, 47.12, -166.07, 4444.26, 408.62 } 
        },{
                { 27, 263, 37.43, 26874, 5547 }, // [2][][]
                { 53, 978, 264, 338, 25287 },
                { 18765, 222, 363.28, 225.29, 12345 }, 
        }
};
for (int x = 0; x < skuskaTroj.length; x++) {
    for (int y = 0; y < skuskaTroj.length; y++) {
        for (int z = 0; z < 5; z++) {
            System.out.println("Hodnota je: " + skuskaTroj[x][y][z]);
        }
    }
}

Also, as you can see, in last for loop I used z < 5 because z < skuskaTroj.length didnt worked for me. 另外,正如你所看到的,在最后一个循环我使用z <5,因为z <skuskaTroj.length对我没有用。 It did not print the whole list. 它没有打印整个列表。 the 4th and 5th number in each line was skipped. 跳过了每一行的第4和第5个数字。 Any idea why? 知道为什么吗?

Thanks ;) 谢谢 ;)

Because your array is 3 x 3 x 5 fields, skuskaTroj.length is 3 (the first part). 因为你的数组是3 x 3 x 5个字段, skuskaTroj.length是3(第一部分)。 If you want to refer to the actual array, the first loop end condition is y < skuskaTroj[x].length and the second z < skuskaTroj[x][y].length . 如果要引用实际数组,则第一个循环结束条件为y < skuskaTroj[x].length ,第二个z < skuskaTroj[x][y].length

real 3-dimensional arrays don't exist in Java. Java中不存在真正的三维数组。 It's just an array of array of array. 它只是一个数组数组的数组。

for (int x = 0; x < skuskaTroj.length; x++){
  for (int y = 0; y < skuskaTroj[x].length; y++){
    for (int z =0; z < skuskaTroj[x][y].length; z++){

Of course you should also check for null if it's possible that any sub-arrays might be null. 当然,你也应该检查null ,如果有可能,任何子阵列可能为空。

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

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