mardi 7 septembre 2021

How from a create list of person with 3 attributes can i loop through the list and get the person name who likes the color 'red' [duplicate]

package com.test.java.collections;

import java.util.ArrayList; import java.util.List;

import com.test.java.getset.Person;

public class StudentList {

public static void main(String[] args) {
    List<Person> personList = new ArrayList<Person>();

    personList.add(new Person("Henry", 25, "Blue"));
    personList.add(new Person("Jessica", 29, "Red"));
    personList.add(new Person("Tom", 21, "Yellow"));
    personList.add(new Person("Zariah", 19, "Red"));
    personList.add(new Person("DeeDee", 29, "Green"));
    
    for(Person person:personList){
        if (person == "Red"){
            String name=personList.get(name);
            System.out.println(personList.get(name));
        }
    }
    
    System.out.println(personList.size());
    
    
}

}

Aucun commentaire:

Enregistrer un commentaire