1
Good afternoon.
I’m learning to use the guys enum
in Java and facing some problems.
First of all, I created my own enum
as follows:
public enum enumUpdateAction {
NEW(0), CHANGE(1), DELETE(2), DELETE_THRU(3), DELETE_FROM(4);
public int codigo;
enumUpdateAction(int codigo) {
this.codigo = codigo;
}
public int getCodigo() {
return this.codigo;
}
}
Now, I need to use such an Enum inside a Switch, in another class:
public static LinkedList<LinkedList<Eventos>> Working(enum_type tipo, enum_updateAction updateAction) {
switch (Eventos.get(0).getUpdateaction()) {
case enum_updateAction.NEW://enum_updateAction.NEW: // 0 = new
ArrayEventos.add(InserirOrdenado(ArrayEventos, Ordem), Ordem);
break;
}
}
The Eventos.get(0).getUpdateaction()
returns a int
. I could use in cases numbers from 0 to 2, but how do I know that each number represents BID
, OFFER
or TRADE
, I’m trying to use these words directly on switch
, with the aid of enum
. Meanwhile, I get that "enum_updateAction.NEW cannot be converted to int". Can anyone tell me how I get the number value of NEW
of enum
, and not the word NEW
in itself?
The
switch(eventos.get(0))
is not an option. ThisEventos.get(0).getUpdateaction
returns an int that I read from a file, being 0 to 4. Depending on what it returns, I take an action (hence the switch). However, when I use NEW directly in the case, a huge list of options for import is presented. I tried to instantiate the ENUM to solve, however the compiler even reported that it is already instantiated. I’m looking for alternatives, but thank you very much for the information. About the language conventions, I’m going back to studying java for now, I’m a little lost yet– Marcos Vinicius
@Marcosvinicius No import needed, just import the class
enum
(that you must already be importing) and nothing more. Skip the list of import suggestions. - When this list appears, press ESC. Also, one of the basic premises ofenum
is to be a fixed and immutable set of objects, and therefore should not be instantiable, as this would cause them to cease to be a fixed and immutable set.– Victor Stafusa
@Marcosvinicius I added an example. See it working here: http://ideone.com/TM8jAM
– Victor Stafusa
Beauty man. Thank you so much for your effort and patience. Now I understand how to work with this
enum
.– Marcos Vinicius