Profess Physics
Profess Physics

Reputation: 317

Filling a multidimensional array in Java using enhanced for loop

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!

Upvotes: 1

Views: 5886

Answers (3)

Developer
Developer

Reputation: 361

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.

Upvotes: 0

Profess Physics
Profess Physics

Reputation: 317

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.

Upvotes: 0

SilverNak
SilverNak

Reputation: 3381

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] = '*';
    }
}

Upvotes: 1

Related Questions