package demo13;import java.util.ArrayList;import java.util.HashMap;import java.util.Set;public class nestdemo {public static void main(String[] args) { HashMap<String,HashMap<String,ArrayList<Student>>> 全國=new HashMap<String,HashMap<String,ArrayList<Student>>>(); //河南地區 HashMap<String,ArrayList<Student>> 河南地區=new HashMap<String,ArrayList<Student>>(); ArrayList<Student> 高校區=new ArrayList<Student>(); Student one=new Student("路人1",18); Student two=new Student("路人2",18); 高校區.add(one); 高校區.add(two); 河南地區.put("河南", 高校區); ArrayList<Student> 低校區=new ArrayList<Student>(); Student one1=new Student("路人3",18); Student two1=new Student("路人4",18); 低校區.add(one1); 低校區.add(two1); 河南地區.put("河南", 低校區); 全國.put("河南地區數據:", 河南地區); //北京地區 HashMap<String,ArrayList<Student>>北京地區=new HashMap<String,ArrayList<Student>>(); ArrayList<Student> 北京高校區=new ArrayList<Student>(); Student one11=new Student("路人1",18); Student two11=new Student("路人2",18); 高校區.add(one11); 高校區.add(two11); 河南地區.put("北京", 北京高校區); ArrayList<Student> 北京低校區=new ArrayList<Student>(); Student one3=new Student("路人3",18); Student two4=new Student("路人4",18); 低校區.add(one3); 低校區.add(two4); 河南地區.put("河南", 北京低校區); 全國.put("北京地區數據:", 北京地區); //遍歷 Set<String> 總key=全國.keySet(); for(String key:總key){ System.out.println(key); HashMap<String, ArrayList<Student>> value=全國.get(key); Set<String> 分地區value= value.keySet(); for(String key2: 分地區value){ System.out.println("來過"); ArrayList<Student> 校區value=value.get(key2); for(Student 學生value: 校區value){ System.out.println("也來過");//此處不顯示 System.out.println("\t"+學生value.getName()+"\t"+學生value.getAge()); } } ? ? }}}
1 回答

慕仰9221625
TA貢獻5條經驗 獲得超1個贊
package?demo13; import?java.util.ArrayList; import?java.util.HashMap; import?java.util.Set; public?class?nestdemo?{ public?static?void?main(String[]?args)?{ HashMap<String,?HashMap<String,?ArrayList<Student>>>?全國?=?new?HashMap<String,?HashMap<String,?ArrayList<Student>>>(); //?河南地區 HashMap<String,?ArrayList<Student>>?河南地區?=?new?HashMap<String,?ArrayList<Student>>(); ArrayList<Student>?高校區?=?new?ArrayList<Student>(); Student?one?=?new?Student("路人1",?18); Student?two?=?new?Student("路人2",?18); 高校區.add(one); 高校區.add(two); 河南地區.put("河南高校區",?高校區); //?##1 ArrayList<Student>?低校區?=?new?ArrayList<Student>(); Student?one1?=?new?Student("路人3",?18); Student?two1?=?new?Student("路人4",?18); 低校區.add(one1); 低校區.add(two1); 河南地區.put("河南低校區",?低校區); //?##原代碼中與(##1)處的key相同,會出現覆蓋 全國.put("河南地區數據:",?河南地區); //?北京地區 HashMap<String,?ArrayList<Student>>?北京地區?=?new?HashMap<String,?ArrayList<Student>>(); ArrayList<Student>?北京高校區?=?new?ArrayList<Student>(); Student?one11?=?new?Student("路人1",?18); Student?two11?=?new?Student("路人2",?18); 北京高校區.add(one11); //?## 北京高校區.add(two11);//?## 北京地區.put("北京高校區",?北京高校區); //?## ArrayList<Student>?北京低校區?=?new?ArrayList<Student>(); Student?one3?=?new?Student("路人3",?18); Student?two4?=?new?Student("路人4",?18); 北京低校區.add(one3); //?## 北京低校區.add(two4); //?## 北京地區.put("北京低校區",?北京低校區); //?## 全國.put("北京地區數據:",?北京地區); //?遍歷 Set<String>?總key?=?全國.keySet(); for?(String?key?:?總key)?{ System.out.println(key); HashMap<String,?ArrayList<Student>>?value?=?全國.get(key); Set<String>?分地區value?=?value.keySet(); for?(String?key2?:?分地區value)?{ System.out.println("??"?+?key2+"----來過");????//?## ArrayList<Student>?校區value?=?value.get(key2); for?(Student?學生value?:?校區value)?{ System.out.println("????學生信息----也來過");//?此處不顯示?## System.out.println("\t"?+?學生value.getName()?+?"\t"?+?學生value.getAge()); } } } } }
備注中帶有##的是當前行有做修改
添加回答
舉報
0/150
提交
取消