How to check and compare possible combinations of array elements

String [] A = {"High","Medium","Low"};
String [] B = {"High","Medium","Low"};
String [] C = {"High","Medium","Low"};
String [] D = {"High","Medium","Low"};
String [] E = {"High","Medium","Low"};
String [] F = {"High","Medium","Low"};

JComboBox Ai = new JComboBox(A); JComboBox Bi = new JComboBox(B);
JComboBox Ci = new JComboBox(C); JComboBox Di = new JComboBox(C);
JComboBox Ei = new JComboBox(E); JComboBox Fi = new JComboBox(F);

....

//add the user choice in arrayList
ArrayList<String> a = new ArrayList<String>();
a.add((String) Ai.getSelectedItem());
a.add((String) Bi.getSelectedItem());
a.add((String) Ci.getSelectedItem());
a.add((String) Di.getSelectedItem());
a.add((String) Ei.getSelectedItem());
a.add((String) Fi.getSelectedItem());

Editorial:
Scenario: There are 6 groups (Ai, Bi, Ci, Di, Ei, Fi). In each group there are 3 selections (High (H), Medium (M), Low (L)). The user needs to choose one of 6 groups

The selection may be, for example, โ€œHHHLLLโ€ or โ€œMMMLLMโ€ or โ€œHHLLMMโ€, etc.

What is the best way to test and match user choices without writing many else if? eg

if(Ai=="High" && Bi=="High" && Ci=="Low" && Di=="High" && Ei=="Low" && Fi=="Medium") {
    System.out.println("Good Choice"); 
}

Thank.

+3
source share
3 answers

First, you do not need to specify a new list of options for each JComboBox.

String[] choices = {"High", "Medium", "Low"};

JComboBox ai = new JComboBox(choices);
JComboBox bi = new JComboBox(choices);
JComboBox ci = new JComboBox(choices);
JComboBox di = new JComboBox(choices);
JComboBox ei = new JComboBox(choices);
JComboBox fi = new JComboBox(choices);

( Java , .)


JComboBoxes . , .

JComboBox[] boxes = {ai, bi, ci, di, ei, fi};

:

// Create an ArrayList of Strings, where each string is either "H", "M", or "L"
ArrayList<String> userChoice = new ArrayList<String>()

for (JComboBox box : boxes) {
    // Go through this code once for each JComboBox in boxes
    // The first time through, "box" means the first JComboBox
    // The second time through, "box" is the second JComboBox, etc.
    if (box.getValue().equals("High")) {
         userChoice.add("H");
    } else if (box.getValue().equals("Medium")) {
         userChoice.add("M");
    } else if (box.getValue().equals("Low")) {
         userChoice.add("L")
    }
}

, . : " , - somethings."


, ArrayList userChoice, - [ "H", "H", "M", "M", "L", "L" ].

+2

.

: Ai * 3^0 + Bi * 3^1 + ... + Fi * 3^5

, , - .

?

0

, enum . == ( ) , . enum (toString valueOf).

, 6 , 3 , 3 ^ 6 . ?

3 ^ 6 . , , , " , , , X".

Map .

enum Setting {
   High, Medium, Low;
}

class SettingCombination {
  List<Setting> combo = ...;
  // should be immutable, with proper equals and hashCode @Override
  // should also have named getters for each of the 6 settings
}

interface SettingCombinationHandler {
   void handle(SettingCombination settings);      
}

class SettingCombinationHandlingService {
   Map<SettingCombination, SettingCombinationHandler> handlers = ...;
   // perhaps provide a default handler as well
}

, . - , ( handle, , ).

, , JComboBox - , . . - ; .

0

Source: https://habr.com/ru/post/1739232/


All Articles