I am very familiar with using Enums in other languages, but I have some difficulties with Java with specific use.
Sun's documentation for Enums boldly states:
"Java programming language variables are much more powerful than their counterparts in other languages, which are more than glorified integers."
Well, this dandy, but I need to have a constant representation of the data type for each of the Enums, for comparison in the switch statement. The situation is this: I am building nodes that will represent a given space or “slot” in the maze graph, and these nodes must be constructed from an array of 2D integers that represent the maze. Here is what I have for the MazeNode class that currently has a problem (switch barks statement):
NOTE. I know that this code does not work due to the dynamic element in the case expression. Here we can illustrate what I need.
public class MazeNode
{
public enum SlotValue
{
empty(0),
start(1),
wall(2),
visited(3),
end(9);
private int m_representation;
SlotValue(int representation)
{
m_representation = representation;
}
public int getRepresentation()
{
return m_representation;
}
}
private SlotValue m_mazeNodeSlotValue;
public MazeNode(SlotValue s)
{
m_mazeNodeSlotValue = s;
}
public MazeNode(int s)
{
switch(s)
{
case SlotValue.empty.getRepresentation():
m_mazeNodeSlotValue = SlotValue.start;
break;
case SlotValue.end.getRepresentation():
m_mazeNodeSlotValue = SlotValue.end;
break;
}
}
public SlotValue getSlotValue()
{
return m_mazeNodeSlotValue;
}
}
, switch: "case- " - , , , , . ?
, Enum 2D- .