gpt4 book ai didi

java - foreach 填充表格不正确

转载 作者:行者123 更新时间:2023-12-01 17:56:55 24 4
gpt4 key购买 nike

我必须填写很多表,所以我制作了表的数组列表。不幸的是,foreach 循环不能正确地增加值(value)。

private List<int[][]> orgnisms; //field in class
public Organisms(int n_tmp,int m_tmp,int mi_tmp) //constructor
{
orgnisms= new ArrayList<>();
newEmptyArrays(); // creates tables 5x5 filled by 0 - works
randomValues();
}
private void randomValues(){
for(int[][] table : orgnisms) // 10 tables
{

table[0][0]=1; // ERROR - should add ONLY to [0][0], but fill other
}
for (int[] ii : table) {
for (int i : ii)
System.out.print(ii[i]);
System.out.println();
}
}
}

对于点[0][0](如上)- 三个值?:

01111 00000 00000 00000 00000

对于点[1][1],方法添加正确:

00000 01000 00000 00000 00000

最佳答案

for (int[] ii : table) {
for (int i : ii)
System.out.print(ii[i]); // <--
System.out.println();
}

这是错误的。在第二个循环中,i 不是 ii 的索引,而是值。

用途:

for (int[] ii : table) {
for (int i : ii)
System.out.print(i); // <--
System.out.println();
}

输出:

10000
00000
00000
00000
00000

关于java - foreach 填充表格不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44053536/

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