r/learnprogramming • u/Eva_addict • 3d ago
I need help understanding a particular case of an improved for loop using an array as a parameter. (Java)
I am learning Java using Deitel's book. This particular for loop uses and array as a parâmeter but I can't understand exactly how it's supposed to work.
int lowGrade = grades[0][0];
for (int[] studentGrades : grades)
{
for (int grade : studentGrades)
{
if (grade < lowGrade)
lowGrade = grade;
}
}For all I understood, the improved for loop works the same as this other example here:
int total;
for (int counter = 0; counter < array.lenght; counter++)
{
total += array[counter];
} But I don't understand how the first one could work in a similar way with an array as a parameter since in the example in the book, the array grades is a bidimentional array. What is that first for loop doing to the int[ ] studentGrades?
The books says something like: " The external improved for loop iterates through the bidimentional array grades assingning new lines to the parameter studentGrades.". How does that even work?
7
u/MagicWolfEye 3d ago
Consider that
is equivalent to
So:
is the same as
4
u/desrtfx 3d ago edited 3d ago
You are dealing with a multi-dimensional array, which, in Java, is just an array of arrays.
So, the outer for-each (enhanced for) loop goes through the first dimension and returns an array of int on each iteration. The inner loop goes through that returned array.
With "conventional" for loops, it would look like this:
You could just as well write the loops as:
There are some nuances in the working mechanisms between conventional and enhanced for loops, though. Enhanced for loops are generally "read only" - meaning that you cannot actually alter the loop iterator (you can alter the contents of an object, or an array, but not the array itself - you cannot reassign an object or array through the iterator). Enhanced for loops use the concept of an "Iterable" and "Iterator" instead of a numeric index. Both, Iterable and Iterator are interfaces in Java: https://www.baeldung.com/java-iterator-vs-iterable
1
u/BranchLatter4294 3d ago
Use print statements to watch what's happening inside the loop.
1
u/mathmage 3d ago
Don't think too much about the 'bidimensional' part.
gradesis an array. Its elements areint[]arrays. The first for loop is iterating through these int arrays, assigning each one the variable namestudentGrades.Inside that for loop, for a given
studentGradesarray, the inner for loop iterates through its elements, which are just ints. These receive the variable namegrade.You can add as many dimensions as you want by nesting more arrays inside arrays, but it doesn't matter. At any given stage, if the object is an array, we can iterate through its elements with a loop. That's all there is to this.
1
u/buzzon 3d ago
Two dimensional array is an array of arrays. When you loop over it, an element is a one dimensional array.
It works like this:
1
u/Available_Safe1818 3d ago
Every time the outer loop executes, studentGrades becomes the next inner array from grades.
Every time the inner loop executes, grade becomes the next int from the array that studentGrades currently is.
If you use print statements to print these things in both loops, you can see it all happening...