如何在java中使用for或while方法调用你的枚举。?

时间:2013-07-21 23:58:44

标签: java arrays enums

所以我试图使用for或while方法,打印出main方法中的所有对象。(Anna,Courtney,Ashley)

         public class JavaApplication56 {

        public enum details {

    Anna(" blue ", " blue"),
    Courtney(" red", " black"),
    Ashley(" yellow ", " green");

    String hairColor;
    String eyeColor;

    details(String hairColor, String eyeColor) {

        this.hairColor = hairColor;
        this.eyeColor = eyeColor;
    }

    public String getHair() {
        return hairColor;

    }

    public String getEye() {


        return eyeColor;
    }
}

我在main方法下遇到错误,该错误表示二元运算符的操作数类型错误。 第一种类型为int,第二种类型为[]。

public static void main(String[] args) {

    JavaApplication56 ja = new JavaApplication56();

    for (int person = 0; person < details.values(); person++) {

        System.out.println(details.values()+details.);
    }

}
}

2 个答案:

答案 0 :(得分:1)

或更简单:

// details should be re-named Details
for (Details detail : Details.values()) {
  System.out.println(detail); // does it have a toString() override?
}

答案 1 :(得分:0)

就像这样:

public static void main(String[] args)
{

    JavaApplication56 ja = new JavaApplication56();

    for (int person = 0; person < details.values().length; person++)
    {
        System.out.println(details.values()[person].name());
    }

}