-3
public int minCompletionTime() { // finds minCompletionTime
  int time = -1;

  for(Job j : jobs) { 
    if (j.getStartTime() == -1) {
      return -1;
    }
  }

  for (Job j : jobs) { //Calculate the minimum completion time
     if (j.getStartTime() + j.time > time) {
         time = j.getStartTime() + j.time;
     }
  }

  return time;
} 

Can someone explain the loop condition? it looks new to for me. Thank you

Mureinik
  • 297,002
  • 52
  • 306
  • 350
FZ-07
  • 5
  • 5

2 Answers2

1

This is an enhanced for statement. The short version - jobs is a collection or an array of Job objects, and the loop iterates over it and assigns a new Job j in each iteration. You could think of it as syntactic sugar equivalent to something like this:

for (int i = 0; i < jobs.size(); ++i) {
    Job j = jobs.get(i);
    // Rest of the loop...

You can read more about this syntax in Oracle's tutorial about the for statement.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

These loops are so-called 'for-each' loops. And their name reflects exactly what they are doing. They execute the task defined in the body for every element in the Collection/array. Let's take a look at a more simple example:

int[] test = new int[] { 5, 7, 9, 3, 4, 9 };
for(int i : test) 
{
  System.out.print( i + " " );
}

The output of this would be :

5 7 9 3 4 9

L.Spillner
  • 1,772
  • 10
  • 19