The meaning of java for loop enumeration:

for(Color c:Color.values())? ? ? ? ? The great god to give pointers

Java Enum enumeration traversal to determine four ways (including Lambda expression filtering)
The following enumeration Color c : Color.values()does not understand this place too much.
Later, after reading these two articles, I understood
that obtaining the value in the color and then taking out the value in the for loop is equivalent to

  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 "";
 	        }
 	       
 	       
 	   } 

Guess you like

Origin blog.csdn.net/weixin_40938312/article/details/105226334