The easy method:
public class BoolArray {
public static void main(String[] args) {
boolean result = checkItem("item1");
System.out.println(result);
}
//or without using return type print false in sysout
public static boolean checkItem(String name){
String items[] = {"item1", "item2", "item3"};
boolean hasItem[] = {true, true, false};
if(name.equals("item1")){
return hasItem[0];
}else if(name.equals("item2")){
return hasItem[1];
}else if(name.equals("item3")){
return hasItem[2];
}else{
return false;
}
}
}
Or you can use advanced methods(like collection API) then you can reduce code lines. You can use HashTable
HashMap
, from these you can work with key, value pairs. Or you can use List with your own custom Pair.
Using HashTable
:
You can implement this as a method and get specific value as above one.
Hashtable table = new Hashtable();
table.put("item1", true);
table.put("item2", true);
table.put("item3", false);
Enumeration e = table.keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(key + " : " + table.get(key));
}