如何检查和匹配 arraylist 元素的可能组合

发布于 2024-08-27 21:29:51 字数 1193 浏览 6 评论 0原文

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

编辑:
设想: 有 6 组(Ai、Bi、Ci、Di、Ei、Fi)可供选择。每组有 3 个子选项(高(H)、中(M)、低(L))。用户需要在 6 个组中每组选择一个。

选项可以是“HHHLLL”或“MMMLLM”或“HHLLMM”等。

在不编写许多 else if 的情况下检查和匹配用户选择的最佳方法是什么? 例如,

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

谢谢。

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

EDITED:
Scenario:
There are 6 groups (Ai,Bi,Ci,Di,Ei,Fi) of choice. On each group, there are 3 sub choice (High(H),Medium(M),Low(L)).The user need to choose one on each of the 6 groups

The choice could be e.g. "HHHLLL" or "MMMLLM" or "HHLLMM" etc.

What is the best way to check and match the user choice without writing many else if ?
e.g.

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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧街凉风 2024-09-03 21:29:51

首先,您不需要为每个 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 中的变量通常以小写字母开头,因此我将变量名称更改为小写。)


接下来,您可以将所有六个 JComboBox 放入一个数组中。稍后您就会明白为什么这很有用。

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")
    }
}

您可能不熟悉 for-each 循环。它的意思是“对某物数组中的每个某物执行一次此代码。”


如果您使用此代码,您最终会得到一个名为 userChoice 的 ArrayList,其中包含类似 ["H", "H", "M", "M", "L", "L"] 的内容。

First off, you don't need to give a new list of choices to 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);

(Variables in Java usually start with a lowercase letter, so I changed the variable names to lowercase.)


Next, you can put all six JComboBoxes in an array. You'll see why this is useful in a moment.

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

Now, you can make your user choice string like this:

// 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")
    }
}

That for-each loop might not be familiar to you. It means "go through this code once for each something in array of somethings."


If you use this code, you'll end up with an ArrayList called userChoice that has something like ["H", "H", "M", "M", "L", "L"].

︶ ̄淡然 2024-09-03 21:29:51

为每个选择分配整数并计算使用选择的一些评估函数。

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

然后您将把用户选择映射到某个整数区间,并且可以更容易地用它做一些事情。

但是,您实际上想用它做什么?

Assign integer to each choice and compute some evaluation function of use choice.

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

Then you would have user choice mapped to certain integer interval and could do something with it easier.

But, what do you actually want to do with it?

枯寂 2024-09-03 21:29:51

首先,您应该使用 enum 来表示 3 个选择。 == (您在字符串上使用的)对于枚举常量是安全的,因为它们保证是单例。 enum 常量可以轻松地与字符串相互转换(toStringvalueOf)。

现在你有 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 或您可能使用的其他内容中显示这些设置选项。这是故意的。您应该将业务逻辑与 UI 分开;来自视图模型的数据模型。

First of all, you should use an enum to represent the 3 choices. The == (that you used on strings) is safe on enum constants, because they're guaranteed to be singletons. An enum constant can be converted to and from strings easily (toString and valueOf).

So now you have 6 variables, and each can have one of 3 values, so there are 3^6 possible scenarios. What's the best way to handle them?

In the most general case, you have to be prepared to handle all 3^6 values independently of each other. That is, I'm assuming you're not doing grouped handling like "as long as there's more Highs than Lows, do X".

I recommend using a Map to map a setting combination to an action object.

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
}

The idea is that for each valid combination, you map a specific handler for it. The mapping need not be one-to-one -- several combinations can be mapped to the same handler if this suits your need (the actual combination is passed as parameter to handle so it can differentiate them if it needs to).

Now, note that I haven't even addressed how you're going to display these setting options in the JComboBox or whatever else you may be using. This is intentional. You should separate your business logic from your UI; your data model from your view model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文