-1

can anyone explain what does it means when you write for(a:b) instead of the normally for(int i =0; i < a; i++). A and b are both variables.

What has a and b in comments?

Here is an example where : is used in a for-loop;

for(PersonalRegistrationData personalRegistrationData : personalRegistrationDataList) {
        if (personalRegistrationData.getEmployeeInitials().equalsIgnoreCase(employeeInitials) &&
                personalRegistrationData.getPersonalActivityID() == personalActivityID && personalRegistrationData.getWeekNumber() == weekNumber) {
            return personalRegistrationData;
        }
    }
starcorn
  • 8,261
  • 23
  • 83
  • 124

1 Answers1

0

The loop: for(int i =0; i < a; i++) is just a classic loop, loop a times to do some things. The loop: for(a: b) (this is the foreach loop in Java) is a loop through a element of a collection. In your example: for(PersonalRegistrationData personalRegistrationData : personalRegistrationDataList) the personalRegistrationData is a collection of PersonalRegistrationData and the personalRegistrationData is an element inside your collection. This loop is useful when you need to get an element from the collection instead of using the first loop with personalRegistrationDataList.get(i)

TuyenNTA
  • 1,194
  • 1
  • 11
  • 19