2013-05-10 399 views
-1
private static Map<String, ArrayList<String>> loadValues = new HashMap<String,ArrayList<String>>(); 
static ArrayList details = new ArrayList<String>(); 

我輸入2組值... say .. key:1值:abc,[email protected],555和key:2值:xyz,x @ z .com,765ArrrayList在檢索鍵值的Hashmap中

我試過了。

System.out.println("Enter the User ID:"); 
     userID = in.next(); 
     System.out.println("Enter your name:"); 
     name = in.next(); 
     details.add(name); 
     System.out.println("Enter your e-mail:"); 
     email = in.next(); 
     details.add(email); 
     System.out.println("Enter your contact number:"); 
     contactNo = in.next(); 
     details.add(contactNo); 
     loadValues.put(userID, details); 

和使用迭代器打印... 當我嘗試打印,其打印像...

1: abc, [email protected], 555 ,xyz,[email protected],765 
2: abc, [email protected], 555 ,xyz,[email protected],765 

但是,我需要打印,1: abc, [email protected], 555 and 2: xyz,[email protected],765 我應該怎麼辦?

+0

您能否提供您用於打印值的代碼? – Smallhacker 2013-05-10 11:01:21

回答

1

這是因爲您沒有爲兩條記錄創建一個新的列表。由於細節是靜態的,因此地圖中的兩條記錄都指向同一個列表。

static List a = new ArrayList(); 
a.add(foo); 
map.put(1, a); 
a.add(bar); 
map.put(2, a); 
map.get(1) == map.get(2) 
>> true 

你需要做的是這樣的:

List a = fillList(); 
List b = fillList(); 
map.put(1, a); 
map.put(2, b); 

其中fillList代表創建表的新實例,並填充它。

+0

loadMap {System.out.println(「輸入用戶名:」); userID = in.next(); System.out.println(「輸入你的名字:」); name = in.next(); details.add(名稱); System.out.println(「輸入你的電子郵件:」); email = in.next(); details.add(電子郵件); loadValues.put(userID,details); } {Map.Entry mEntry =(Map.Entry)iterator.next(); System.out.println(mEntry.getKey()+「:」+ mEntry.getValue());} – user2369645 2013-05-10 11:17:52

+0

Danstahr:是的..可以簡單介紹一下嗎?但我動態給出結果集值。對於列表,它可能會增加,因爲我的記錄行增加了嗎? – user2369645 2013-05-10 11:24:28