1

I hope you all are doing well, so the problem I guess is in understanding the difference between those two codes:

first code (filling multidim-array with enhanced for loop) - does not work

static char[][] battleBoard = new char[10][10];
public static void main(String[] args)
{


    for(char[] rows:battleBoard)
    {
        for(char column:rows)
        {
            column = '*';
        }
    }


}

if we tried to print-out the array (expecting '*' to be assigned to each element in the array) using the following code - we fail (i.e it is still empty)!

for(int i=0;i<battleBoard.length;i++)
    {
        for(int j=0; j<battleBoard[i].length; j++)
        {
            System.out.print("|" + battleBoard[i][j] + "|");
        }
        System.out.println();


    }

But on the other hand we have(it works fine when printed):

static char[][] battleBoard = new char[10][10];
public static void main(String[] args)
{


    for(char[] rows:battleBoard)
    {
        Arrays.fill(rows, '*');
    }

So the question is: does not column, rows in the previous code-blocks stand for new variables - i.e Arrays.fill(rows,'*') shouldn't have met the need cause it fills ((rows char array)) but not ((battleBoard char array)) ;

Thx in advance for helping me out!

1
  • The first example doesn't work because it doesn't update the array, just a value you got from the array. The second example works because you are calling code which has been implemented correctly, so no surprise there. I am not clear on what your question is. Commented Mar 8, 2017 at 9:26

3 Answers 3

1

The first method does not fill the array, because in the code

for (char column : rows) {
    column = '*';
}

the value in the array is copied to the new variable column. You then just assign a new value for this variable, but the element in the array remains unchanged.

The method Arrays.fill() does not copy the value, instead it operates directly on the array. You can do so yourself with this code (which is basically the same as Arrays.fill() does):

for (int i = 0; i < battleBoard.length; i++) {
    for (int j = 0; j < battleBoard[i].length; j++) {
        battleBoard[i][j] = '*';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thx for giving a hand here!. but if you could bear with me when you said "The method Arrays.fill() does not copy the value, instead it operates directly on the array.", here " the array" refers to multidimensional array battleBoard I guess, but then why doesn't it refer to one-dimensional array rows!!, in other words, why doesn't Arrays.fill() deal with the argument "rows" as a separated -array- variable in which case (it is merely a copy) >> battleBoard's values won't change<< just as the logic is applied in part one where "column" is being dealt with as a separated -single- variable!
Sorry for not making this clear. Arrays.fill() operates on the array you give as an argument. So in your case every call of this function fills one row of the two-dimensional battleBoard. The difference here is: rows is a reference to one row in the battleBoard. column is a copy of one cell in the array
0

I'd like to thank SilverNak for his answer. Here is my final view of the answer (detailed) - for the ones who are searching:

the difference between column and rows is that "column" is a single primitive variable that represents a cell in the multidimensional array (say: battleBoard), but when it comes to "rows" it is a reference variable to an object (the one-dimensional array (say: battleBoard[i]) where i belongs to the interval [0, battleBoard.length), and when it comes to different references x,y,z where y=x, z=y, then any change to be done on one of them(say z) -except for putting it to null- would affect all the others because they refer to the same object. I've made a simple analogy with the following 2-code blocks:

first is when we use primitive variables (like "column" in our previous question)

int x = 3;
int y = 4;

y = x;
y = 10;
System.out.print(x + " ");
System.out.println(y);

The result would be printing 3 10

-- Notice: changing one does not change the other because they are INDEPENDENT COPIES of each other.

second is when we use reference variables (like "rows" in our previous question)

int[] test1 = new int[5];
int[] test2 = test1;
test2[2] = 3;
System.out.print(Arrays.toString(test1) + " ");
System.out.print(Arrays.toString(test2));

The result would be printing [0, 0, 3, 0, 0] [0, 0, 3, 0, 0]

--Notice: changing one does change the other because the actual change was done to the object (array in our case); and test1, test2 are merely references to the same object(array) and they are NOT INDEPENDENT COPIES of each other.

Comments

0

I think the answers here are waaaaaaay to complicated. The simplest reason is that in the first code you are using a for-each loop. The purpose of this loop is to get you a reference to each element in the array one at a time. So when you say

for(char column:rows){
    column = '*';
}

column is a variable that displays the value of the current element and does not actually give you access to write to that element in the array. If you wanted it to work you should instead reverse your loops, use the for-each to print and the double for-loop to write to the array. Also Arrays.fill() simply does what it says which is why it works.

1 Comment

Thx for your contribution. I still think there is a misunderstanding. Our concern here is (not the functionality of the enhanced for loop) but rather simply "the difference between column and rows as two different references", so finally it turned out that those dudes are just different ! one is a primitive and the other is a reference to an object!. So it is not the for-each loop(enhanced for loop)'s mistake nor the Arrays.fill() 's.., and this is I think more general to be pointed out for a deeper understanding!

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.