0

I was converting a bunch of ordinary for loops to for-each loops in my most recent project and ran into a paradoxical problem.

    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.

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.

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.

I hope I have provided enough information.

IAmThePiGuy

2
  • thanks everyone who answered so quickly, it still amazes me the warmth of this online community. BTW, if someone could come up with a way to make the nested for set into a nested for-each set similarly I would be grateful. Commented Dec 15, 2010 at 9:16
  • All has been solved and happy compiler! Commented Dec 15, 2010 at 9:36

7 Answers 7

5

In the first loop, you need c to be an int so that you can access rowArray[c] - but this should work:

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]:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow... I warranted an answer by THE Jon Skeet! Thanks, that's EXACTLY what I wanted.
4

you're trying to use r, which is an int [], as just int. It is wrong. fix it to

for (int c : r) {
  ...
}

Comments

3
int[] c : imgMap[r]

as index of array it takes int and you have supplied int[]

It should be

   for (int[] r : imgMap) {
        for (int c : r) {
            System.out.print(" " + c);
        }
        System.out.println("");
    }

Comments

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.

Comments

3
for (int[] r : imgMap) {
    for (int c : r) {
        System.out.print(" " + c);
    }
    System.out.println("");
}

Comments

3

If i understand this correct it has to be like this:

for (int[] r : imgMap) {
        for (int c : r) {
            ...
        }
        ...
    }

Comments

2

You'd need to code

for (int c : r)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.