2013-05-10 54 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