0

複数の 2D 配列を検索し、配列内のデータを検索して、メソッドが呼び出されたときにメソッドに入れられるデータと比較できるようにする必要があります。私は使用しようとしましたが、検索しているすべての配列がこのタイプであることを.getClass().getSimpleName()返すだけint[][]なので、それらを区別するのに役立ちません。

この一連のコードを使用して、呼び出す適切な配列を決定しています。

 public void tempSelection78 (int fRate, int wbTemp, int rcTons, int a, int r)
  {
    setTitle(TONS_05_78.getClass().getSimpleName());
     for (int model = 0; model < 7; model++)
    {
      String tonChart = tons78FCharts[model];
      for (int col = 0; col < 9; col++)
      {
        for (int row = 0; row < 8; row++)
        {
          int t = 0;

          if (tonChart.equals(TONS_03_78.getClass().getSimpleName()))
          {
            t = TONS_03_78[col][row];
          }
          if (tonChart.equals(TONS_04_78.getClass().getSimpleName()))
          {
            t = TONS_04_78[col][row];
          } 
          if (tonChart.equals(TONS_05_78.getClass().getSimpleName()))
          {
            t = TONS_05_78[col][row];
          }
          if (tonChart.equals(TONS_07_78.getClass().getSimpleName()))
          {
            t = TONS_07_78[col][row];
          }
          if (tonChart.equals(TONS_09_78.getClass().getSimpleName()))
          {
            t = TONS_09_78[col][row];
          }
          if (tonChart.equals(TONS_11_78.getClass().getSimpleName()))
          {
            t = TONS_11_78[col][row];
          }
          if (tonChart.equals(TONS_15_78.getClass().getSimpleName()))
          {
            t = TONS_15_78[col][row];
          }
          if (rcTons == t)
          {
            tableButton = new JButton(new ImageIcon(tablesFor78[model], tablesFor78[model]));
            break;
          }
          else 
          {
            tableButton = new JButton(new ImageIcon("CANNOT_FIND_MODEL.GIF", "SCROLL"));
          }
        }
      }
    }
    for (int model = 0; model < 7; model++)
    {
      String flowChart = flow78FCharts[model];
      for (int col = 0; col < 9; col++)
      {
        for (int row = 0; row < 8; row++)
        {
          int t = 0;
          if (flowChart.equals(FLOW_03_78.getClass().getSimpleName()))
          {
            t = FLOW_03_78[col][row];
          }
          if (flowChart.equals(FLOW_04_78.getClass().getSimpleName()))
          {
            t = FLOW_04_78[col][row];
          } 
          if (flowChart.equals(FLOW_05_78.getClass().getSimpleName()))
          {
            t = FLOW_05_78[col][row];
          }
          if (flowChart.equals(FLOW_07_78.getClass().getSimpleName()))
          {
            t = FLOW_07_78[col][row];
          }
          if (flowChart.equals(FLOW_09_78.getClass().getSimpleName()))
          {
            t = FLOW_09_78[col][row];
          }
          if (flowChart.equals(FLOW_11_78.getClass().getSimpleName()))
          {
            t = FLOW_11_78[col][row];
          }
          if (flowChart.equals(FLOW_15_78.getClass().getSimpleName()))
          {
            t = FLOW_15_78[col][row];
          }
          if (fRate == t)
          {
            tableButton = new JButton(new ImageIcon(tablesFor78[model], tablesFor78[model]));
            break;
          }
          else 
          {
            tableButton = new JButton(new ImageIcon("CANNOT_FIND_MODEL.GIF", "SCROLL"));
          }
        }
      }
    }

私がしたいのは、tonChart検索可能な2次元配列を含む文字列の1つに設定することです。ただし、.getClass().getSimpleNameこれ以外に設定されることはなくint[][]、プログラムは常に、各トリプル ネスト ループの最後にある else ステートメントに迂回します。

助言がありますか?

呼び出される配列の例を次に示します。

public final int [][] TONS_03_78 = 
  {{27, 32, 37, 41, 45, 48, -1, -1, -1},
    {-1, 41, 45, 50, 55, 59, 64, 68, 70},
    {-1, 49, 55, 60, 65, 70, 75, 80, 85}, 
    {-1, -1, 64, 70, 75, 81, 86, 91, 97},
    {-1, -1, -1, 80, 86, 92, 98, 103, 109},
    {-1, -1, -1, -1, 97, 103, 109, 116, 122},
    {-1, -1, -1, -1, -1, 114, 122, 128, 135},
    {-1, -1, -1, -1, -1, -1, -1, -1, -1}};
  public final int [][] FLOW_03_78 = 
  {{205, 160, 137, 122, 112, 103, -1, -1, -1},
    {-1, 203, 170, 150, 137, 127, 120, 113, 105},
    {-1, 245, 205, 180, 163, 150, 140, 133, 127}, 
    {-1, -1, 240, 210, 188, 173, 162, 152, 145},
    {-1, -1, -1, 240, 215, 197, 183, 172, 163},
    {-1, -1, -1, -1, 242, 220, 205, 193, 183},
    {-1, -1, -1, -1, -1, 245, 228, 213, 202},
    {-1, -1, -1, -1, -1, -1, -1, -1, -1}};
4

2 に答える 2

3

配列をマップに入れることができます:Map<String, int[][]>

次に、マップに正しいペアを設定できますmap.put("TONS_09_78", TONS_09_78)

そして、あなたの一連のifは(+いくつかのnull処理)になります:

return map.get(tonChart)[col][row];

設計が疑わしいように見えますが、最初に配列を宣言して、配列を扱いやすくするためのより良い方法があるかもしれません。

于 2012-07-03T15:33:38.470 に答える
0

私はあなたのパラダイムを変更します。

あなたの特定の問題が何であるか完全にはわからないので、次のアプローチは変わるかもしれませんが、もし私があなただったら、2次元配列とある種の識別子を含むクラスまたは構造体を構築します。(文字列、int、列挙型など、必要なものは何でもかまいません)。

この種のアプローチは、コード サイズを縮小するだけでなく、あなたと私たちの両方にとってより読みやすくなります。

于 2012-07-03T15:36:35.460 に答える