2

I have some C++ code I am porting to Java.

My code looks something like this@

enum direction {UP, DOWN, LEFT, RIGHT, NEUTRAL};
int LINK[5];
.....
//fill LINK array
.....
if (desired_direction == LEFT  ) || (LINK[LEFT]  > 0) return true;
if (desired_direction == RIGHT ) || (LINK[RIGHT] > 0) return true;
if (desired_direction == UP    ) || (LINK[UP]    > 0) return true;
if (desired_direction == DOWN  ) || (LINK[DOWN]  > 0) return true;

So I have an array filled with ints and I am using an enum for the index of the array.

What is the Java way of going about this?

Thanks

2
  • You're missing the () chars in your if statements even for C++. Commented Dec 12, 2011 at 1:48
  • yeah . I edited the code for clarity before I posted the question and forgot to re-add the parenthesis. Commented Dec 12, 2011 at 20:47

4 Answers 4

2

Basically, nothing is different.

In fact the only things missing here are the parenthesis around the two conditions of each if statements.

Also, your call to the enum value will be direction.whatever.ordinal() instead of just whatever.

Sign up to request clarification or add additional context in comments.

4 Comments

this is not right, you can not use an enum as a index on an int array
you would need to call Direction.Left.ordinal()
@NimChimpsky thanks. I forgot that enums aren't naturally ordinals.
Just to clarify, I have not tested it, but it seems that the code IS the same except "if (desired_direction == LEFT ) || (LINK[LEFT] > 0) return true;" becomes "if (desired_direction == direction.LEFT ) || (LINK[direction.LEFT.ordinal()] > 0) return true;" etc (and yes, there are missing parethensis around the if statements
2

You could use an EnumSet instead of the integer array. Enums in Java do have an integer associated with each instance (obtained from the ordinal() method, which you could use to keep the above code as it is), and EnumSet is implemented using a long as a bitfield (well, that's RegularEnumSet, and there's an alternative implementation for enums with more than 64 members- choosing is encapsulated).

So sth like:

EnumSet<Direction> links = EnumSet.noneOf(Direction.class);
// populate links (links.add(Direction.UP) etc)
if (desired_direction == LEFT || links.contains(Direction.LEFT)) return true;
// etc...

Comments

1

You could iterate over a direction[]:

direction[] enumVals = direction.values();
for(direction dir: enumVals) {
    if ((dir == direction.LEFT  ) || (LINK[dir.ordinal()]  > 0))
        return true;
}

enumName.values() returns an enumName[] that holds all the elements of the enum.

Note that you cannot use an enum-element directly as a number. You need to call (for example) direction.LEFT.ordinal() to get the 0-based ordinal of the element in the enum.

1 Comment

replace "direction.LEFT" by desired_direction if you want it to do the same as the original code.
1

Your enum could like this:

public enum Direction {
    UP(0), DOWN(1), LEFT(2), RIGHT(3), NEUTRAL(4);
    private Integer index;

    Direction(Integer index){
        this.index = index;
    };

    public Integer getIndex(){
        return this.index;
    }
}

and then your if statements :

Direction desired_direction = Direction.LEFT;
int[] LINK = {2,0,0,0,0};
if (desired_direction == Direction.LEFT  || LINK[Direction.LEFT.getIndex()]  > 0){
 return true;
}

Or you could reference the ordinal() method rather than explicity creating an index Integer. Like so LINK[Direction.LEFT.ordinal()] > 0

However maybe using a hashmap could be appropriate (althoguh the logic is not quite the same as your question :

Map<Direction, Integer> myLink = new HashMap<Direction, Integer>(5);
        myLink.put(Direction.LEFT, 2);
        myLink.put(Direction.RIGHT, 0);
        myLink.put(Direction.UP, 0);
        myLink.put(Direction.DOWN, 0);

        if(myLink.get(desired_direction) > 0) 
        {
            return true;
        }

But my favourite is putting all the logic on the enum itself and then passing the int array as a parameter :

public Boolean getAnalysis(int[] link){
  return link[this.getIndex()]  > 0 ? Boolean.TRUE:Boolean.FALSE; 
}

I also like explicit setting an index property rather than rely on the ordinal, just in case someone alters the enum in the future and your call would like this

Direction desired_direction = Direction.LEFT;
return desired_direction.getAnalysis(LINK);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.