2

So I'm trying to make a class that encapsulates a 2D array of chars. In particular, what I want to do is define the default constructor so that the encapsulated 2D array contains the default char ('#' in this case). My problem: when I attempt to systematically fill the array with the default char via nested foreach loops, the compiler doesn't acknowledge that I am using the second nested loop's initialized char parameter c, although I obviously am with the assignment c = '#'. To quote Eclipse, "The value of the local variable c is not used."

Here is the relevant code:

public class 2DCharArr
{
 private char[][] chars;
 private byte length_x, length_y;

 public 2DCharArr()
 {
  length_x = 10; length_y = 10;
  chars = new char[length_x][length_y];
  for (char[] arr : chars)
   for (char c : arr)
    c = '#'; // Does not signal to Eclipse that c was used.
 }
}

Is there something wrong with my syntax in the foreach loops, so that the compiler should fail to acknowledge my use of c? It would be great for someone to clear this up for me, since it is keeping me from using foreach loops to create objects that contain multi-dimensional arrays, though I feel like I should be able to if I'm ever to be very competent with the language. Thanks in advance for the insight!

sirsuripu
  • 63
  • 1
  • 6

2 Answers2

4

You can't assign values while using an enhanced-for statement. You're going to have to iterate over them the conventional way and assign values into specific indexes.

for(int i= 0; i < chars.length; i++) {
    for(int j = 0; j < chars[i].length; j++) {
        chars[i][j] = '#';
    }
}

The reason for this is nuanced in the way that the enhanced for is defined in the JLS:

  • If it's an Iterable, it uses its iterator with the result of next() bound to your variable.
  • If it's an array type, it creates a for-loop with the index value bound to the variable.

In either case, you can't modify the variable being passed in to any meaningful effect, such as assigning values.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Ah, I see ... quite enlightening, but also a bit confusing, as it certainly defies the "it's just a for-loop for arrays" idea that's alleged by just about every Java tutorial out there. Now as an amateur I likely shouldn't judge the great minds who invented Java, but I think that if you're gonna make such an intuitive notation as the foreach loop, then you should design it to comply with expected intuitions. And certainly the intuition "I can do this (e.g. assignment) in a for loop, so I should be able to do this also in a foreach loop" is a frequent one to have, right? It's quite a bummer... – sirsuripu Oct 01 '14 at 02:25
  • Well, for starters, it's *not* a for loop for just arrays; you can use this for anything that is `Iterable` in addition to an array. The idea is also to read an enhanced-for as pertaining to each element in the array instead of every position in the array. That may be the mistake that the tutorials are making. The [Java Trails](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) makes it clear that the variable used holds a single value in the array. – Makoto Oct 01 '14 at 03:16
1

You are confusing the use of assignments:

int x = 5;
int y = x;
y = 6;

would not change the value of x. Your inner loop is essentially "saying":

char c = arr[current index];
c = '#';

This would not change the value of arr[current index], it would simply change the value of c to '#', but leave the original arr array unchanged. The compiler is warning you that the variable you are creating c is never used (meaning never read).

Note that this loop would work just fine:

for (char[] arr : chars)
    for (int x = 0; x < arr.length; x++)
        arr[x] = '#';

The outer foreach loop assigns the arr variable to point to the current char[] object within chars. Then arr[x] assignes the x-th elementh with the value '#'.

xpa1492
  • 1,953
  • 1
  • 10
  • 19
  • It's interesting as well as aggravating to me how the notations in a language can carry such inconspicuous specifications along with them. But I guess knowing those implications is what separates the men from the boys in this field, so I shouldn't complain too much. Thanks for the tip! – sirsuripu Oct 01 '14 at 02:39