我想从 arraylist 中获取随机项。我的代码如何尝试不工作。
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_ADD, parser.getValue(e, KEY_ADD)); --edited code
menuItems.add(map);
int index = random.nextInt(menuItems.size());
HashMap<String, String> itm = menuItems.get(index);
System.out.println(itm);
编辑:
int index = random.nextInt(menuItems.size());
HashMap<String, String> itm = new HashMap<String, String>();
itm = menuItems.get(index);
String randomstring = itm.get(MyClass.KEY_NAME);
System.out.println(randomstring);
收到 NullPointerException