java打印元素列表,这是HashMap的值
我创建了一个HashMap<String,String[]>
我试图在每个值的列表元素旁边打印键
例如,第一行应该打印
English : Alex Lamplough Kayleigh Lamplough Bella Lamplough
我的代码:
public class Students {
private HashMap <String, String[]> subjects;
public Students() {
subjects = new HashMap<String, String[]>();
}
public void initialDetails() {
this.subjects.put("English", new String[] {"Alex Lamplough", "Kayleigh Lamplough", "Bella Lamplough"});
this.subjects.put("Maths", new String[] {"Bill Burr", "Kayleigh Lamplough", "Jake Stanford"});
this.subjects.put("Science", new String[] {"Paul Hatton", "Jake Stanford", "Bill Burr"});
this.subjects.put("IT", new String[] {"Jake Stanford", "Alex Lamplough", "Julie King"});
this.subjects.put("Sports", new String[] {"Barbara Kensington", "Bella Lamplough", "Alex Lamplough"});
this.subjects.put("Languages", new String[] {"Julie King", "Harry Milner", "Bella Lamplough"});
this.subjects.put("History", new String[] {"Harry Milner", "Kayleigh Lamplough", "Bill Burr"});
Set set = this.subjects.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry) iterator.next();
System.out.println(mentry.getKey() + " : " + mentry.getValue());
}
}
}
# 1 楼答案
也许更好的方法是,当我们得到键值时,如果我们可以使用for循环,然后我们可以使用HashMap值作为字符串数组
当然,我们用一个for-each来打印元素
输出
完整代码