Results 1 to 11 of 11
Thread: Enumerated constants
- 01-27-2011, 04:07 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Enumerated constants
I have a class that is an enum that i'm using as a bunch of tokens for some parsing. When my program is finished with the parsing, it outputs to an excel style spreadsheet. Currently, the column heads of this spreadsheet are hard-coded, as strings, and require the user to add them by hand in the correct order. Since there are only 10 or so column heads, this is not a problem. But it will become one, and out of order, missing, or incorrect column heads seem like they will be part of my not-too-distant future if I allow this to continue. So. What i'd like to do is build a piece of code that takes the enumerated constants in the class in the order they are declared, turns each one into a string, and puts it into a list I can use later (I prefer ArrayLists). How would I go about doing this? I'm only somewhat familiar with reflection, so I may need some hand-holding. I can't post the code in question, only similar examples. Thanks in advance for any advice, tutorials, suggestions, etc. If its tutorial responses, be aware that i've already googled my way through the first two pages of "turning enumerated constants into a list of strings."
- 01-27-2011, 04:31 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Why do you want them as a list of Strings?
Why not simply use the enum itself?
You can get the ordered set using EnumSet.
As for turning each one into a string, then simply use toString(). You can override that as well, if you don't simply want to use the name of the enum.
And getting them? Use valueOf().
- 01-27-2011, 05:05 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
As strings, so that they can be used as the column heads in the excel spreadsheet. I'm not going to use the enum itself, as that has the same pitfalls as writing out the columnheads long form like I have it now.
So ClassName.THISTOKEN.toString() would be worthless -- it incurs the same problems.
What i'm looking for is to create an ArrayList or something similar of every single enum in the Class, in order. So that I needn't make changes to the code each time I add a new Token.
- 01-27-2011, 05:11 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
I still don't understand how that helps.
How do these tokens (your enum) relate to the column headings?
How does having a separate list of Strings help?
Indeed, how is your idea of converting the enums into a list of Strings any different than simply using the EnumSet and toString on the enums?
This is th bit that I don't understand:
That list will be the same as an EnumSet of your enum.What i'd like to do is build a piece of code that takes the enumerated constants in the class in the order they are declared, turns each one into a string, and puts it into a list I can use later (I prefer ArrayLists).
- 01-27-2011, 05:12 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Hang on.
This is an enum you're talking about isn't it and not a class with a load of static constants in it?
- 01-27-2011, 07:46 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Have you tried parameterized enum?
Hi,
I am not sure I understand your question but see my example below.
I have a "Tokens" enum with names {A.B.C} but the values they hold are "nice" values so when you do Tokens.A.toString() you don't get "A" rather than "Nice name 1".
================================================== =
== BEGIN
public class MainThread extends TestCase {
public static void testEnum() {
final List<Tokens> tokens = Arrays.asList(Tokens.values());
for (Tokens token : tokens) {
System.out.println(token);
}
}
private static enum Tokens {
A ("Nice name 1"),
B ("Nice name 2"),
C ("Nice name 3");
private final String niceName;
private Tokens(String niceName) {
this.niceName = niceName;
}
@Override
public String toString() {
return this.niceName;
}
}
}
== END
================================================== =
Running this code above will result in the following output:
Nice name 1
Nice name 2
Nice name 3
- 01-28-2011, 12:39 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
I'm going to try Golan's code, it looks kind of like what I was trying to put together myself.
However, I'm all for learning new things, so let me see if I understand this:
I'm not familiar with enumsets, and in looking through the java specs, I'm not sure I understand how one could make use of that either --
Would an enumset of my Tokens class, for example
enumSet<Tokens> create a set of all of the tokens, in a Set format?
So I would just be able to for:each through the created set, outputting each with toString() ?
- 01-28-2011, 03:53 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
So I would just be able to for:each through the created set, outputting each with toString() ?
You don't even need an enum set for that. Just use the values() method:
Java Code:for(TheEnum e :TheEnum.values()) { System.out.println(e); }
Of course you can do other things instead of printing them ... like making them the column headers of your table.
- 01-28-2011, 08:42 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
So, is this answered then?
You can just use the enum without having to create some additional List.
- 01-29-2011, 12:37 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Wow, I totally didn't know you could iterate through them like this -- where did you pick up this bit of information? i'm interested in more like it.
Second question, while i'm on the topic of iteration and reflection -- Is there a simple way to call only the get members of a class, one after the other in the order they're declared?
- 01-29-2011, 01:50 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
where did you pick up this bit of information? i'm interested in more like it.
The 1.5 guide notes are a bit more detailed than the section in Oracle's Tutorial. (Although it is worth reading, and does mention the values() method.)
There are API docs for EnumSet and EnumMap of course.
Finally, dry, technical and not guaranteed to be easily understandable, but - by definition - correct and complete is the Java Language Specification: 8.9 Enums.
I mention all these because they are pretty much where I go if I can't understand something. (Not always though: there are better glosses for, say, generics.) Whether the JLS is an appropriately new-to-java resource is a matter of debate. But knowing that it exists, and knowing your way around it is worthwhile: just don't get hung up on it. The API docs, although daunting, are to be consulted: from day one and on each day thereafter.
[Edit] Sorry - just noticed that this isn't the NTJ forum! Doesn't change things much: some people would look anywhere else for information and leave the JLS to language lawyers. But it is an important resource and only sometimes inpenetrably dense.Last edited by pbrockway2; 01-29-2011 at 01:54 AM.
Similar Threads
-
Enumerated Types
By muhsy in forum Advanced JavaReplies: 2Last Post: 01-27-2011, 08:18 AM -
Problem using enumerated types
By Stryker4526 in forum New To JavaReplies: 3Last Post: 10-10-2009, 03:57 AM -
Brainbox needed -- KeyEvent VK_* constants stable??!
By Spiller in forum AWT / SwingReplies: 0Last Post: 08-17-2009, 03:59 PM -
How to define constants in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:39 PM -
Global constants
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 09:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks