简体   繁体   English

Java在case语句中使用变量

[英]Java using variables in case statement

So, I know you can't use a variable in the case statement. 因此,我知道您不能在case语句中使用变量。 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). (例如,我可以做一堆ifs)。

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). 数组中的位置由一个名称给出,如下所示(int ... ordin语句)。 Basically I have to assign generate 'result' objects for certain members of the array (if they are discrete data such as C_VENT_RATE). 基本上,我必须为数组的某些成员分配“生成”结果对象(如果它们是离散数据,例如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)). 我可以很容易地看到这一点的唯一方法是执行诸如if(i.equals(pr_int))之类的ifs。

  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;
      }

Since you already have an enum, you can try the Command pattern using an EnumMap mapping your enum to a Command. 由于已经有了枚举,因此可以使用将枚举映射到Command的EnumMap尝试使用Command模式。

Each Command instance will be the same logic as one of your case statements. 每个Command实例将与您的case语句之一具有相同的逻辑。

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();
    }
}

Do like this way 这样喜欢

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

If the object contains an enum (UncEKG) as a variable, why not just use the switch on the enum 如果对象包含枚举(UncEKG)作为变量,为什么不使用枚举上的开关

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 枚举信息

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). 因此,您可以在enum类中添加一个静态方法以基于int获取正确的列,然后可以基于enum进行切换,或者执行其他操作,或者最好在enum上有一个方法,说明是否列是否已处理(尽管如果枚举更通用,则可能不合适)。

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;
       }
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM