String to Enum

Hallo ich glaube ich hatte dich missverstanden :)

Du meintest String -> enum

Die lösung von mir ist enum -> String was ja trivial ist :)

Hier nochmal diesmal von String -> enum :

Code:
class StringToEnumConverter{
    enum farben{BLAU, GRUEN};
    public static void main(String[] args){
        farben t = farben.GRUEN;
        t = Enum.valueOf(t.getDeclaringClass(), "BLAU");
    }
}

Sorry dafür


//Wenns dann erledigt sein sollte mach ein Haken ans Thema. Danke
Gruß

RedWing
 
Zuletzt bearbeitet:
Hallo!

Schau mla hier:
Code:
 /**
  * 
  */
 package de.tutorials;
 
 /**
  * @author Tom
  * 
  */
 public class EnumExample {
 
 	private enum Color {
 		RED, GREEN, BLUE
 	};
 
 	/**
 	 * @param args
 	 */
 	public static void main(String[] args) {
 		for (Color c : Color.values()){
 			System.out.println(c);
 		}
 		
 		Color color = Color.valueOf("RED");
 		System.out.println(color);
 	}
 }

Gruß Tom
 
lol doch so einfach :)

Woher kommt das ?.valueOf(String) eigentlich?
Das hab ich mich bei der length eines Arraytypen auch schon immer gefragt.

Gruß

RedWing
 
Hallo!

Aus:
Code:
 	 private enum Color {
 		 RED, GREEN, BLUE
 	 };

Erzeugt der Java Compiler:
Code:
 // Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov  Date: 03.01.2006 20:44:00
 // Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
 // Decompiler options: packimports(3) 
 // Source File Name:   EnumExample.java
 
 package de.tutorials;
 
 
 // Referenced classes of package de.tutorials:
 //			EnumExample
 
 private static final class EnumExample$Color extends Enum
 {
 
 	public static final EnumExample$Color[] values()
 	{
 		EnumExample$Color aenumexample$color[];
 		int i;
 		EnumExample$Color aenumexample$color1[];
 	    System.arraycopy(aenumexample$color = ENUM$VALUES, 0, aenumexample$color1 = new EnumExample$Color[i = aenumexample$color.length], 0, i);
 		return aenumexample$color1;
 	}
 
 	public static final EnumExample$Color valueOf(String s)
 	{
 		EnumExample$Color aenumexample$color[];
 		EnumExample$Color enumexample$color;
 		for(int i = (aenumexample$color = ENUM$VALUES).length; --i >= 0;)
 		    if(s.equals((enumexample$color = aenumexample$color[i]).name()))
 				return enumexample$color;
 
 		throw new IllegalArgumentException(s);
 	}
 
 	public static final EnumExample$Color RED;
 	public static final EnumExample$Color GREEN;
 	public static final EnumExample$Color BLUE;
 	private static final EnumExample$Color ENUM$VALUES[];
 
 	static 
 	{
 		RED = new EnumExample$Color("RED", 0);
 		GREEN = new EnumExample$Color("GREEN", 1);
 		BLUE = new EnumExample$Color("BLUE", 2);
 		ENUM$VALUES = (new EnumExample$Color[] {
 			RED, GREEN, BLUE
 		});
 	}
 
 	EnumExample$Color(String s, int i)
 	{
 		super(s, i);
 	}
 }

Gruß Tom
 
Zurück