在LinkedList中使用枚举值

时间:2015-06-06 06:47:30

标签: java enums

我正在尝试将性别值传递给LinkedList,但它会出错。

public class Employee{

    private String name;
    private String last;
    private Gender gender;
    private int ID;

    Employee(int ID, String name, String last, Gender gender){

        this.name=name;
        this.last=last;
        this.gender=gender;
        this.ID=ID;
    }

    public enum Gender {
        M,F
    }
}


public static void main(String[] args){

    List<Employee> oopClass = new LinkedList<>();

    oopClass.add(100,"Mick","tayson",M);
    oopClass.add(101,"Jonny","Tayson",F );
    oopClass.add(108,"Barack","Obama",F);
    oopClass.add(105,"Vlad","Putin",M);
}

2 个答案:

答案 0 :(得分:1)

您应该将Employee的实例添加到List:

oopClass.add(new Employee(100,"Mick","tayson",M));
oopClass.add(new Employee(101,"Jonny","Tayson",F));
oopClass.add(new Employee(108,"Barack","Obama",F));
oopClass.add(new Employee(105,"Vlad","Putin",M));

答案 1 :(得分:1)

您需要将Employee的实例添加到链接列表中。所以改变

oopClass.add(100,"Mick","tayson",M);

oopClass.add(101,"Jonny","Tayson",F );

 oopClass.add(108,"Barack","Obama",F);

oopClass.add(105,"Vlad","Putin",M);

oopClass.add(new Employee(100,"Mick","tayson",M));

oopClass.add(new Employee(101,"Jonny","Tayson",F ));

oopClass.add(new Employee(108,"Barack","Obama",F));

oopClass.add(new Employee(105,"Vlad","Putin",M));