Rob
Rob

Reputation: 2472

Java using variables in case statement

So, I know you can't use a variable in the case statement. I am hoping someone can point me to code that would be fairly efficient as a replacement. (I could do a bunch of ifs, for example).

The situation is that I have an array of object data, and I want to iterate through that array. The position in the array is given by a name as shown below (the int...ordinal statements). Basically I have to assign generate 'result' objects for certain members of the array (if they are discrete data such as C_VENT_RATE). The only way I can see this done easily is do a bunch of ifs such as if (i.equals(pr_int)).

  ArrayList<String[]> rawEKGs  = ekgFile.getForMrno( docInfo.getMedicalRecordNumber() );

  for (String[] parts : rawEKGs) {
    for (int i=0; i< parts.length; i++ )
    {
      Result result = docInfo.getResult();
      boolean process = true;
      final int vent_rate = UncEKG.COL_NAMES.C_VENT_RATE.ordinal();
      int art_rate = UncEKG.COL_NAMES.C_ART_RATE.ordinal();
      int pr_int = UncEKG.COL_NAMES.C_PR_INTERVAL.ordinal();
      int qrs_dur =  UncEKG.COL_NAMES.C_QRS_DURATION.ordinal();
      int qt_qtc =  UncEKG.COL_NAMES.C_QT_QTC.ordinal();
      int prt =  UncEKG.COL_NAMES.C_PRT_AXES.ordinal();


      switch(i) {
        case : // something
          break;
        default: process = false;
      }

Upvotes: 1

Views: 191

Answers (4)

dkatzel
dkatzel

Reputation: 31648

Since you already have an enum, you can try the Command pattern using an EnumMap mapping your enum to a Command.

Each Command instance will be the same logic as one of your case statements.

EnumMap<UncEKG.COL_NAMES, Command> map = ...
//values is in ordinal order
//pulled out for performance reasons
UncEKG.COL_NAMES[] names = UncEKG.COL_NAMES.values();
for (String[] parts : rawEKGs) {
    for (int i=0; i< parts.length; i++ ){
       map.get(names[i]).execute();
    }
}

Upvotes: 4

Yishai
Yishai

Reputation: 91881

I think the right way is to put the logic in the enum. So you would add a static method to the enum class to get the right column based on the int, and then you can switch based on the enum, or perhaps do something else, or even better have a method on the enum which says if that column is processed or not (although that may not be appropriate if the enum is more general purpose).

Quick and dirty would look like this, though:

 ArrayList<String[]> rawEKGs  = ekgFile.getForMrno( docInfo.getMedicalRecordNumber() );
 UncEKG.COL_NAMES[] values = UncEKG.COL_NAMES.values();
 for (String[] parts : rawEKGs) {
    for (int i=0; i< parts.length; i++ )
    {
       Result result = docInfo.getResult();
       boolean process = true;
       switch (values[i]) {
           case UncEKG.COL_NAMES.C_VENT_RATE:
                break;
           default: process = false;
       }
    }
 }

Upvotes: 1

Beanz
Beanz

Reputation: 210

If the object contains an enum (UncEKG) as a variable, why not just use the switch on the enum

switch (theEnum) {
    case UncEKG.COL_NAMES.C_VENT_RATE:
        //something
        break;
    case UncEKG.COL_NAMES.C_PR_INTERVAL:
        //something else
        break;
    default: process = false
}

Enum info

Upvotes: 1

KhAn SaAb
KhAn SaAb

Reputation: 5366

Do like this way

    switch(i) {
        case 0: // something
          break;
        case 1: // something
          break;
        case 2: // something
          break;
         .
         .
         .
         .
        default: process = false;
      }

Upvotes: 1

Related Questions