"for"루프를 "for of"루프로 바꾸려면 어떻게합니까? [복제]

Nov 28 2020

이 for 루프를 각 루프로 바꾸려고합니다.

        for (int i = 0; i < appointments.size(); i++) {
            if (appointments.get(i).equals(appointment)) { 
              appointments.get(i).setAvailability(true);
            }
        }
   

답변

ElliottFrisch Nov 28 2020 at 18:46

for-each appt에서 appointments. 처럼,

for (Appointment appt : appointments) {
    if (appt.equals(appointment)) {
        appt.setAvailability(true);
    }
}

또는, 8 + 자바를 사용하여 수 있다면 . 처럼,streamappointments

appointments.stream()
        .filter(x -> x.equals(appointment))
        .forEach(appt -> appt.setAvailability(true));