有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    也许更好的方法是,当我们得到键值时,如果我们可以使用for循环,然后我们可以使用HashMap值作为字符串数组

    String [] names = (String[]) entry.getValue();
    

    当然,我们用一个for-each来打印元素

        for(Map.Entry entry : subjects.entrySet()){
                System.out.print("key " + entry.getKey() + " : ");
                String [] names = (String[]) entry.getValue();
                for(String name : names){
                    System.out.print(name + " ");
                }
                System.out.println();
            }
    
        }
    }
    

    输出

    key English : Alex Lamplough Kayleigh Lamplough Bella Lamplough 
    key Maths : Bill Burr Kayleigh Lamplough Jake Stanford 
    key Science : Paul Hatton Jake Stanford Bill Burr 
    key Languages : Julie King Harry Milner Bella Lamplough 
    key IT : Jake Stanford Alex Lamplough Julie King 
    key History : Harry Milner Kayleigh Lamplough Bill Burr 
    key Sports : Barbara Kensington Bella Lamplough Alex Lamplough 
    

    完整代码

        public class Students {
    
        private HashMap<String, String[]> subjects;
    
        public Students() {
    
            subjects = new HashMap<String, String[]>();
    
        }
    
        public static void main(String[] args) {
    
            Students s = new Students();
    
            s.initialDetails();
        }
    
        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());
    //        }
    
    
            for(Map.Entry entry : subjects.entrySet()){
                System.out.print("key " + entry.getKey() + " : ");
                String [] names = (String[]) entry.getValue();
                for(String name : names){
                    System.out.print(name + " ");
                }
                System.out.println();
            }
    
        }
    }