pour (Color c: Color.values ())? ? ? ? ? Le grand dieu pour donner des pointeurs
Parcours d'énumération Java Enum pour déterminer quatre façons (y compris le filtrage d'expression Lambda)
L'énumération suivante Color c : Color.values()
ne comprend pas trop cet endroit.
Plus tard, après avoir lu ces deux articles, j'ai compris
qu'obtenir la valeur dans la couleur, puis retirer la valeur dans la boucle for équivaut à
Color[] cs = Color.values();
for (int i=0;i<=cs.length;i++) {
if(cs[i].getIndex()==index){
return cs[i].getName();
}
}
public static String getname(int index){
Color[] cs = Color.values();
for (int i=0;i<=cs.length;i++) {
//for (Color c : Color.values()) {
if(cs[i].getIndex()==index){
// if(c.getIndex()==index){
return cs[i].getName();
// return c.getName();
}
}
return "";
}
public enum Color
{
RED("s",1),GREEN("s",2),WHITE("s",3),YELLOW("s",4);
private Color(String name,int index)
{
this.name=name;
this.index=index;
}//是对应上面的组合
private String name;
private int index;
//构造方法
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String Name) {
this.name = name;
}
public static String getname(int index){
Color[] cs = Color.values();
for (int i=0;i<=cs.length;i++) {
//for (Color c : Color.values()) {
if(cs[i].getIndex()==index){
// if(c.getIndex()==index){
return cs[i].getName();
// return c.getName();
}
}
return "";
}
}