Wie verwandle ich meine "for" -Schleife in eine "for of" -Schleife? [Duplikat]

Nov 28 2020

Ich versuche, diese for-Schleife für jede Schleife zu machen.

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

Antworten

ElliottFrisch Nov 28 2020 at 18:46

for-each apptin appointments. Mögen,

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

Oder wenn Sie Java 8+ verwenden, können Sie streamdas appointments. Mögen,

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