[英]how do I compare members of an array list against a string?
I am so sorry for this simple question (I have only been working with java for a little bit). 对于这个简单的问题,我感到非常抱歉(我只使用过Java一点时间)。
I have an Array List of check boxes and they have a corresponding label with it from another Array List. 我有一个复选框的数组列表,并且它们具有另一个数组列表中的相应标签。
i want to be able to set a string variable to a certain value depending on what the label of the selected check box is. 我希望能够根据所选复选框的标签将字符串变量设置为某个值。 I need to compare up to four selected check boxes.
我需要比较最多四个选定的复选框。 So if check box A, with label A = X, then the serial number of that device = Y.
因此,如果带有标签A = X的复选框A,则该设备的序列号=Y。
I have tried to use the contains() method, but it returns null. 我尝试使用contains()方法,但它返回null。 I have tried to use get(index).getText(), but that only works for a single index (when I have up to four that I need to evaluate).
我曾尝试使用get(index).getText(),但这仅适用于单个索引(当我需要评估多达四个时)。
Any ideas and suggestions are welcome. 欢迎任何想法和建议。
Thanks!! 谢谢!!
ironmantis7x Ironmantis7x
=================================================================== ================================================== =================
Here is a part of my code with the suggestion below: 这是我的代码的一部分,并带有以下建议:
if (PlatformPanel.Android.isSelected() == true)
{
Iterator<JCheckBox> listIter = Devices.selectedDevices.iterator();
while(listIter.hasNext())
{
JCheckBox nextItemInList = listIter.next();
if (nextItemInList.toString().equals("HTC Droid Eris"))
//if (Devices.selectedDevices.iterator().toString() .equals("HTC Droid Eris"))
{
selectedSerial = "A100000DA78159";
System.out.println(selectedSerial);
System.out.println(Devices.selectedDevices.get(0));
}
if (nextItemInList.toString().equals("Asus Transformer Prime (#1)"))
//if (Devices.selectedDevices.iterator().toString() .equals("Asus Transformer Prime (#1)"))
{
selectedSerial = "BKOKAS127271";
System.out.println(selectedSerial);
System.out.println(Devices.selectedDevices.get(0));
}
}
}
I would use a static map ( static
means it's a class field, rather than an instance field, so all instances get to re-use the one map): 我将使用静态映射(
static
意味着它是一个类字段,而不是实例字段,因此所有实例都可以重用一个映射):
public class MyClass {
private static Map<String, String> serialNumbers = new HashMap<String, String>() {{
put("HTC Droid Eris", "A100000DA78159");
put("Asus Transformer Prime (#1)", "BKOKAS127271");
// etc
}};
// rest of class
}
then in your code, it's a one-liner: 然后在您的代码中,它是单线的:
String selectedValue = ...; // get it directly from the drop down
String selectedSerial = serialNumbers.get(selectedValue);
You may Iterate over the arraylist and compare with String. 您可以遍历arraylist并与String比较。
Example: Assuming your list is of type String
. 示例:假设您的列表的类型为
String
。
Iterator<String> listIter = yourList.iterator();
while(listIter.hasNext())
{
String nextItemInList = listIter.next();
if(nextItemInList.equals("YourOtherString")
{
//Do your logic.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.