Reputation: 1514
I have to define ENUMs for all Switch cases. I am not sure how to relate enums when it comes to positions. Below is my code:
public enum Choice {
A, B, C
}
public void selectItem(int position) {
switch (position) {
// Dashboard
case 0:
break;
case 1:
break;
}
}
Upvotes: 2
Views: 228
Reputation: 13485
you can try adding some methods into your enum to contain the logic.
public enum Choice {
A, B, C;
static Choice[] values = Choice.values(); //so array not created defined every time.
public static Choice fromOrdinal(int position){
if(position<values.length){
return values[position];
}
return A; //default
}
}
// for example when a spinner is selected in android, it returns an int position.
// to map this position to an enum, you can call the fromOrdinal() method like below.
public void selectItem(int position) {
switch (Choice.fromOrdinal(position)) {
// Dashboard
case A:
break;
case B:
break;
...// the rest of your cases.
}
}
Upvotes: 2
Reputation: 65793
Many good answers here. Just adding one more that is quite elegant and worth considering.
You can make the enum
do all of the work.
interface Selectable {
public void selected();
}
public enum Choice implements Selectable {
A {
@Override
public void selected() {
System.out.println("We're all going to die!!!");
}
}, B{
@Override
public void selected() {
System.out.println("Everyone lives forever");
}
}, C{
@Override
public void selected() {
System.out.println("Nobody knows.");
}
};
}
This actually completely removes the the need for a switch ... case
statement.
Upvotes: 0
Reputation: 696
Generally, this is bad idea. However to convert an ordinal into its enum represantation you might want to do this:
Choice value = Choice.values()[position];
Below you can find clipping from the Item 31: Use instance fields instead of ordinals (Effective Java, page 158, Joshua Bloch), where described how to avoid using ordinals.
Never derive a value associated with an enum from its ordinal; store it in an instance field instead
// Abuse of ordinal to derive an associated value - DON'T DO THIS public enum Ensemble { SOLO, DUET, TRIO, QUARTET, QUINTET, SEXTET, SEPTET, OCTET, NONET, DECTET; public int numberOfMusicians() { return ordinal() + 1; } } public enum Ensemble { SOLO(1), DUET(2), TRIO(3), QUARTET(4), QUINTET(5), SEXTET(6), SEPTET(7), OCTET(8), DOUBLE_QUARTET(8), NONET(9), DECTET(10), TRIPLE_QUARTET(12); private final int numberOfMusicians; Ensemble(int size) { this.numberOfMusicians = size; } public int numberOfMusicians() { return numberOfMusicians; } }
Upvotes: 3
Reputation: 1505
You will need to transform int position to respective ENUM. Every enum value has a ordinal by default. Here in Choice enum, A has ordinal zero and so on.
public class TestMain {
public enum Choice {
A, B, C
}
public static void main(String[] args) {
selectItem(0);
}
public static void selectItem(int position) {
Choice selectedChoice = null;
selectedChoice = getChoiceFromPosition(position, selectedChoice);
switch (selectedChoice) {
//Dashboard
case A:
System.out.println(selectedChoice.ordinal());
break;
case B:
break;
case C:
break;
}
}
private static Choice getChoiceFromPosition(int position, Choice selectedChoice) {
for(Choice c : Choice.values()){
if(c.ordinal() == position) {
selectedChoice =c;
}
}
return selectedChoice;
}
}
Upvotes: 1
Reputation: 348
public enum Choice {
A, B, C
}
public Choice selectItem(int position) {
return Choice.values()[position];
}
Upvotes: 2
Reputation: 48258
do instead use the enum directly in the switch
public void selectItem(Choice x) {
switch (x) {
// Dashboard
case A:
break;
case B:
break;
}
}
Upvotes: 5