I want to convert the following traditional for loop to foreach loop:
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j]=sc.nextInt();
My attempt is:
for(int[] innerArr: arr)
for(int ele: innerArr)
ele = sc.nextInt();
This doesn't work. I thought that since innerArr represents a row of the array, and ele represents a single element in that row, the above code would work. But I guess only the existing value of that element in arr[i][j] is copied into ele. Is it even possible to assign a value to that element using a foreach loop?