I was converting a bunch of ordinary for loops to for-each loops in my most recent project and ran into a paradoxical problem.
在我最近的一个项目中,我正在将一堆普通的for循环转换成for-each循环,并遇到了一个矛盾的问题。
imgMap = new int[rows][cols];
for (int r=0; r<imgMap.length; r++) {
rowArray = mapBR.readLine().split(",");
for (int c=0; c<imgMap[r].length; c++) {
imgMap[r][c] = Integer.parseInt(rowArray[c]);
}
}
System.out.println(imgMap.length+", "+imgMap[0].length);
// print array in rectangular form
for (int[] r : imgMap) {
for (int[] c : imgMap[r]) {
System.out.print(" " + c[0]);
}
System.out.println("");
}
imgMap is a two-dimensional int array (int[][]
) to hold a 'map'
mapBR is a BufferedReader of the external file that the 'map' is taken from
The first nested for loop set reads the file, the second nested for-each set writes it to the console to make sure that it is read correctly.
int imgMap是一个二维数组(int[][])举行“地图”mapBR是BufferedReader外部文件的“地图”是来自第一个嵌套循环读取文件,第二组嵌套for - each写控制台,以确保它是正确读取。
I couldn't see a way to make the first for loop set work as for-each loops so that is a sub-problem I would be delighted if someone could help me with as well.
我不知道如何使第一个for循环集作为for-each循环工作,所以如果有人也能帮助我,我将很高兴这是一个子问题。
Anyway, back to the main problem. when I (try to) compile this the compiler regurgitates an error saying that the int[] c : imgMap[r]
line has incompatible types, but, and here's the catch, when I change it to int c : imgMap[r]
, it coughs up the same error! As I can't see how it could be any other than one of those types I am flummoxed.
不管怎样,回到主要问题。当我(尝试)编译它时,编译器会返回一个错误,说int[] c: imgMap[r]行有不兼容的类型,但是,当我将它改为int c: imgMap[r]时,它会产生同样的错误!因为我看不出它怎么会是其他类型的,我感到困惑。
I hope I have provided enough information.
我希望我已经提供了足够的信息。
IAmThePiGuy
IAmThePiGuy
7 个解决方案
#1
4
In the first loop, you need c
to be an int
so that you can access rowArray[c]
- but this should work:
在第一个循环中,您需要c为int,以便您可以访问rowArray[c]——但是这应该起作用:
for (int[] row : imgMap) {
String[] rowArray = mapBR.readLine().split(",");
for (int c = 0; c < row.length; c++) {
row[c] = Integer.parseInt(rowArray[c]);
}
}
In the second loop, you just need to iterate through r
, not through imgMap[r]
:
在第二个循环中,你只需要遍历r,而不是通过imgMap[r]:
for (int[] row : imgMap) {
for (int value : row) {
System.out.print(" " + value);
}
System.out.println("");
}
Basically, you need to think carefully about the types involved - whether a variable represents an index into an array, or the array itself.
基本上,您需要仔细考虑所涉及的类型——变量是表示数组中的索引,还是数组本身。
#2
4
you're trying to use r, which is an int []
, as just int
. It is wrong. fix it to
你想用r,一个int[],作为int,这是错的。修复它
for (int c : r) {
...
}
#3
3
int[] c : imgMap[r]
as index of array it takes int
and you have supplied int[]
作为数组的索引,它需要int并且你已经提供了int[]
It should be
它应该是
for (int[] r : imgMap) {
for (int c : r) {
System.out.print(" " + c);
}
System.out.println("");
}
#4
3
This is how it works:
这就是它的工作原理:
for (int[] row : imgMap) {
for (int cell : row) {
System.out.print(" " + cell);
}
System.out.println("");
}
The outer for loop provides all rows from the matrix, the inner for loops all cells from a row.
外部for循环提供来自矩阵的所有行,内部for循环来自一行的所有单元格。
#5
3
for (int[] r : imgMap) {
for (int c : r) {
System.out.print(" " + c);
}
System.out.println("");
}
#6
3
If i understand this correct it has to be like this:
如果我理解正确的话,就应该是这样:
for (int[] r : imgMap) {
for (int c : r) {
...
}
...
}
#7
2
You'd need to code
你需要的代码
for (int c : r)
#1
4
In the first loop, you need c
to be an int
so that you can access rowArray[c]
- but this should work:
在第一个循环中,您需要c为int,以便您可以访问rowArray[c]——但是这应该起作用:
for (int[] row : imgMap) {
String[] rowArray = mapBR.readLine().split(",");
for (int c = 0; c < row.length; c++) {
row[c] = Integer.parseInt(rowArray[c]);
}
}
In the second loop, you just need to iterate through r
, not through imgMap[r]
:
在第二个循环中,你只需要遍历r,而不是通过imgMap[r]:
for (int[] row : imgMap) {
for (int value : row) {
System.out.print(" " + value);
}
System.out.println("");
}
Basically, you need to think carefully about the types involved - whether a variable represents an index into an array, or the array itself.
基本上,您需要仔细考虑所涉及的类型——变量是表示数组中的索引,还是数组本身。
#2
4
you're trying to use r, which is an int []
, as just int
. It is wrong. fix it to
你想用r,一个int[],作为int,这是错的。修复它
for (int c : r) {
...
}
#3
3
int[] c : imgMap[r]
as index of array it takes int
and you have supplied int[]
作为数组的索引,它需要int并且你已经提供了int[]
It should be
它应该是
for (int[] r : imgMap) {
for (int c : r) {
System.out.print(" " + c);
}
System.out.println("");
}
#4
3
This is how it works:
这就是它的工作原理:
for (int[] row : imgMap) {
for (int cell : row) {
System.out.print(" " + cell);
}
System.out.println("");
}
The outer for loop provides all rows from the matrix, the inner for loops all cells from a row.
外部for循环提供来自矩阵的所有行,内部for循环来自一行的所有单元格。
#5
3
for (int[] r : imgMap) {
for (int c : r) {
System.out.print(" " + c);
}
System.out.println("");
}
#6
3
If i understand this correct it has to be like this:
如果我理解正确的话,就应该是这样:
for (int[] r : imgMap) {
for (int c : r) {
...
}
...
}
#7
2
You'd need to code
你需要的代码
for (int c : r)