tags:

views:

30

answers:

2

I want to store some user preferences Would Enum be a good datastructure to store them? If so, how do I do it? This is the final usage of the datastructure.

main(){
  int my_val = PREFERENCES.BLACK;
  switch(PREFERENCES){

   case BLACK:
     ...
}
+1  A: 

Typically, preferences for a program have a variety of data types: Your windows positions are usually integers expressing X,Y positions; default file or directory names may be strings, color values would usually be stored as java.awt.Color values, and so on. enums would come in handy only for values that take on one of a small number of predefined possibilities, and those possibilities aren't values that are naturally represented by integers, characters, strings, floats, colors or other more specialized data types. A good use for an enum would be the position of a toolbar: { TOP, LEFT, BOTTOM, RIGHT, CENTER } or something like that.

Enums are their own data type; you don't store their values in ints (or you'd be talking about final int fields and not enums). You create some enum variables of the type you want, and you can basically only assign one of the type's fixed values to it. You can then later do a switch statement on enum variables, though.

Carl Smotricz
A: 

For a fix set of selectable items, Enum is a good choice. Depending on your application, you could use a EnumSet (at runtime) or some kind of serialization (as persistence) for storing it.

An easy way for serializing Enums is using their numerical value, retrievable by Enum.ordinal()

PartlyCloudy