Switch Statement for Enum Value Representations in Java -


I am very familiar with using anem in other languages, but I have some difficulty in Java with a special use is.

Sun documentation for anoes is boldly said:

"Java programming language enmoys are far more powerful in other languages ​​compared to their counterparts, which are slightly more than glorified integers Are. "

Well, it's dandy, but for comparative reasons in a switch statement, I need to continuously represent datatype for each anome. The situation is as follows: I am creating nodes which will show a given location, or 'slot' in a maze graph, and these nodes can be made from a 2D integer array representing the maze. What I have found for the majnode class is that which is currently the problem (switch statement bark):

Note: I know that this code does not work because of dynamic item case In the statement of this is to clarify what I am after. Public square mesanode {public enum slot value {empty (0), start (1), wall (2), visited (3), end (9); Private Int M_ Presentation; Slot value (int rendering) {m_representation = representation; } Public int getRepresentation () {return m_representation; }} Private SlotView m_mazeNodeSlotValue; Public mezznode (slotwalks) {m_mazeNodeSlotValue = s; } Public mezznode (int s) {switch (s) {case slot value mp.representation (): m_mazeNodeSlotValue = SlotValue.start; break; Case SlotValue.end.getRepresentation (): m_mazeNodeSlotValue = SlotValue.end; break; }} Public Slot Value getSlotValue () {return m_mazeNodeSlotValue; }}

then complains to the code switch statement, "case expression must be continuous expression" - I can see why the compiler may have trouble, technically they are dynamic , But I'm not sure what approach will it take to solve this, is there any better way?

The bottom line is required to have a uniform integer value to compare against the 2D array of frequencies in the program.

You can use something like this:

  Import java.util.HashMap; Import java.util.Map; Public class meznode {public enum slot value {empty (0), start (1), wall (2), visited (3), end (9); Protected int m_representation; Slot value (int rendering) {m_representation = representation; } Private stable last map & lt; Integer, slot value & gt; Mapping = new hashmap & lt; Integer, slot value & gt; (); Stable {for (slot value slot value: slot value.values ​​()) {mapping. Input (slotv.m_ rendering, slot value); }} Report from the public fixed slot value {slot value slot value = slot valuemapping. Gat (rendering); If (slot value == blank) / / throw an exception of your own throw a new runtime exception ("Invalid representation:" + representation); Return slot value; }} Private SlotView m_mazeNodeSlotValue; Public mezznode (slotwalks) {m_mazeNodeSlotValue = s; } Public majonode (int s) {m_mazeNodeSlotValue = SlotValue.from Report (s); } Public Slot Value getSlotValue () {return m_mazeNodeSlotValue; } Public static zero main (string [] args) {Mesonode M = new Mesonode (2); Println (m.getSlotValue ()); M = new maznode (9); Println (m.getSlotValue ()); }}  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -