java - 3 维数组中带有嵌套 for 循环的整数

标签 java arrays for-loop

问题是:

在 main 方法中,创建一个 10x10x10 三维数组。

通过使用嵌套的for循环,存储其每个坐标位置的总和值。

例如:位置 (3,4,8) = 3 + 4 + 5 = 15

这是我的代码:

import java.util.Arrays;

public class threedim
{

    public static void main(String args[])
    {

   int threeD[][][] = new int[10][10][10];    
   int i, j, k = 0;


        for(i=0; i<10; i++)
            for(j=0; j<10; j++) {
                threeD[i][j] = k;
                k++;
        }


        for(i=0; i<10; i++) {
            for(j=0; j<10; j++)
                System.out.print(threeD[i][j] + " ");
           System.out.println();
        }
      System.out.println();

   }

}

我收到不兼容的类型:int 无法转换为 int[] 错误,有人可以帮助我吗?

提前谢谢你:)

最佳答案

由于您有一个三维数组,因此需要三个嵌套循环:

int h, i, j, k = 0;

for (h=0; h<10; h++) 
    for(i=0; i<10; i++)
        for(j=0; j<10; j++) {
            threeD[h][i][j] = k; // Is that really, what you want to do?
            // threeD[h][i][j] = h + i + j; seems to be the right thing to do according to your explanation
            k++;
        }

for (h=0; h<10; h++) 
    for(i=0; i<10; i++) {
        for(j=0; j<10; j++)
            System.out.print(threeD[h][i][j] + " ");
       System.out.println();
    }

关于java - 3 维数组中带有嵌套 for 循环的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41100096/

相关文章:

java - Spring JPA 没有将自定义值枚举类型保存到数据库

java - Android 中的海绵城堡

java - Spring Batch 自定义编写器 close 方法被调用两次(需要特殊处理)

c - 当在c中输入空行时如何使此代码中的循环停止

c# - 从数组构建 if 条件

java - 为什么当记录 > 约 600 条时查询性能急剧下降

php - 避免在 PHP 中显示来自数据库的重复结果

javascript - 将 for 循环变成静态数组?

c - 求函数时间复杂度的准确方法

java - For循环从自定义ArrayList获取数据