gpt4 book ai didi

java - Java 打印多维数组

转载 作者:行者123 更新时间:2023-12-01 20:25:10 25 4
gpt4 key购买 nike

我只想通过 for 循环打印我的空数组。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

出了什么问题?

int NYEARS = 5;
int NRATES = 3;

double[][] balancee = new double[NYEARS][NRATES];
for (int i = 0; i < NYEARS; i++) {
for (int j = 0; j < NRATES; j++) {
System.out.print(balance[NYEARS][NRATES] + " ");
System.out.println();
}
}

最佳答案

您应该使用循环索引来访问数组元素,而不是数组维度:

for (int i = 0; i < NYEARS; i++) {
for (int j = 0; j < NRATES; j++) {
System.out.print(balance[i][j] + " ");
System.out.println();
}
}

关于java - Java 打印多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43961778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com