Note that there are some explanatory texts on larger screens.

plurals
  1. POJava For-each loop data type mismatch
    primarykey
    data
    text
    <p>I was converting a bunch of ordinary for loops to for-each loops in my most recent project and ran into a paradoxical problem. </p> <pre><code> imgMap = new int[rows][cols]; for (int r=0; r&lt;imgMap.length; r++) { rowArray = mapBR.readLine().split(","); for (int c=0; c&lt;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(""); } </code></pre> <p><strong>imgMap</strong> is a two-dimensional int array (<code>int[][]</code>) to hold a 'map'<br> <strong>mapBR</strong> is a BufferedReader of the external file that the 'map' is taken from<br> 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.</p> <p>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.</p> <p>Anyway, back to the main problem. when I (try to) compile this the compiler regurgitates an error saying that the <code>int[] c : imgMap[r]</code> line has incompatible types, but, and here's the catch, when I change it to <code>int c : imgMap[r]</code>, <em>it coughs up the same error!</em> As I can't see how it could be any other than one of those types I am flummoxed.</p> <p>I hope I have provided enough information.</p> <p><em><strong>IAmThePiGuy</em></strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload