Skip to content
Advertisement

Tag: enums

Get index of enum from string?

I have a string value, I also have an array of strings and an enum containing the range also. To get the index of the string in the array, from the value supplied I write this: How do I do this for an enum? Can I use ordinal? Or do i have to make my own method? The method might

How to implement enum with generics?

I have a generic interface like this: This interface has limited instances, hence it would be best to implement them as enum values. The problem is those instances have different type of values, so I tried the following approach but it does not compile: Any idea about this? Answer You can’t. Java doesn’t allow generic types on enum constants. They

Printing the enum’s name

I’m using eclipse + Android SDK on Ubuntu. I would like to print the name of a sensor type device, there a a lot of them and I want to do it automatically. If I use a I print the (int) type, but I would like the name of the enum. How could I do that? Answer For enumerations, you

Java enum elements with spaces?

Im working on java, I have created an enum as follows: Above example gives errors while using space in the name of element (i.e. north America). Any suggestions how to resolve above issue? Answer You can’t put a space in the middle of an identifier. Doing so ends that identifier and the parser assumes whatever comes next is a valid

How to convert enum value to int?

I have a function which return a type int. However, I only have a value of the TAX enumeration. How can I cast the TAX enumeration value to an int? Answer You’d need to make the enum expose value somehow, e.g. (I’ve changed the names to be a bit more conventional and readable, btw.) This is assuming you want the

Finding Java Enum types from code values?

We use code values in our database, and Enums in Java. When querying the database, we need to take a code value and get an Enum instance. Is it overkill to have a HashMap to avoid iteration? What would you do? Is there an easier way? Answer That’s exactly the approach I’d take to solve that particular problem. I see

Java’s enum… Where are they created?

Since enum in C# are on the stack, I was wondering where enum, in Java, where created. On the stack? On the heap? In some mysterious other place? Enumeration in C# are more primitive than those in Java, this might explain why they are created on the stack… Where are they? I can’t find them! Thanks Answer Enums in Java

Advertisement