亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

最后到遍歷學生時遍歷不出來???

最后到遍歷學生時遍歷不出來???

實力走天下 2017-05-28 18:48:55
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());
				}
			}

		}
	}
}

備注中帶有##的是當前行有做修改

查看完整回答
反對 回復 2017-05-29
  • 實力走天下
    實力走天下
    ##原代碼中與(##1)處的key相同,會出現覆蓋,沒有相同啊, 控制臺不顯示一個學生信息 最后這一段好像沒運行,for (Student 學生value : 校區value) { System.out.println(" 學生信息----也來過");// 此處不顯示 ## System.out.println("\t" + 學生value.getName() + "\t" + 學生value.getAge()); } }
  • 慕仰9221625
    慕仰9221625
    1.我說的覆蓋是指 【河南地區.put("河南", 高校區);】 和 【河南地區.put("河南", 低校區);】 這兩條語句put到相同的key(河南)上,不修改的話在最后取值時,這一塊的數據只能取到低校區的學生。 2.看到沒有運行是因為前面的賦值有些問題,導致有些集合沒賦值,所以是空集合,也就沒有執行。你可以按照調整后的代碼跑一遍,應該是可以看到有結果的。
  • 1 回答
  • 0 關注
  • 1497 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號