JAVA LIST 出错
现在有两个数组,codeList和drugnameList ,要实现的功能是adapter加入不可重复的code,相同的code,drugsname依次增加,最后要实现的结果是code=1drugname=青霉素紫霉素,code=2drugname=红霉素绿霉素code=3drugname=蓝霉素黑霉素,可是我的代码目前实现的结果是
code=1drugname=青霉素紫霉素
code=2drugname=红霉素
code=3drugname=蓝霉素
code=2drugname=绿霉素
code=3drugname=黑霉素
请教下是怎么回事?
String [] codeList = {"1","2","3","2","3","1"};
String [] drugnameList = {"青霉素","红霉素","蓝霉素","绿霉素","黑霉素","紫霉素"};
List<HashMap<String, String>> adapter = new ArrayList<HashMap<String, String>>();
a: for(int i=0;i<codeList.length;i++){
System.out.println("int i = "+i);
if(adapter.size()==0){
System.out.println("************ adapter.size =0 ");
HashMap<String, String> drugs = new HashMap<String, String>();
drugs.put("code", codeList[i]);
drugs.put("drugname", drugnameList[i]);
adapter.add(drugs);
}else{
System.out.println(adapter.size());
b: for (int j=0;j<adapter.size(); j++) {
System.out.println("************ it.hasNext() ");
HashMap<String, String> map = adapter.get(j);
String code = map.get("code");
System.out.println("******** code ="+code);
System.out.println("******** codeList ="+codeList[i]);
if (code.equalsIgnoreCase(codeList[i])) {
System.out.println("************ map reput ");
String drugname = map.get("drugname");
map.put("drugname", drugname + drugnameList[i]);
// break b;
} else {
System.out.println("************ adapter add ");
HashMap<String, String> drugs = new HashMap<String, String>();
drugs.put("code", codeList[i]);
drugs.put("drugname", drugnameList[i]);
adapter.add(drugs);
// break b;
}
}
}
}
Iterator<HashMap<String, String>> it=adapter.iterator();
for(;it.hasNext();) {
HashMap<String, String> map =it.next();
String name= map.get("code");
String drugname= map.get("drugname");
System.out.println("code=" +name +"drugname=" +drugname);
}
}
------解决方案--------------------String [] codeList = {"1","2","3","2","3","1"};
String [] drugnameList = {"青霉素","红霉素","蓝霉素","绿霉素","黑霉素","紫霉素"};
List<HashMap<String, String>> adapter = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for(int k=0;k<codeList.length;k++){
if(map == null
------解决方案-------------------- map.size()<=0
------解决方案--------------------map.get(codeList[k])==null
------解决方案--------------------map.get(codeList[k]).equalsIgnoreCase("")){
map.put(codeList[k], drugnameList[k]);
}else{
String tmp = map.get(codeList[k]);
String value = tmp.concat(drugnameList[k]);
map.put(codeList[k], value);
}
}
adapter.add(map);