日期:2014-05-20  浏览次数:20765 次

Vector的get数据转换成hashmap,只能成功一条?
Vector allpromotion = (Vector)pageContext.getAttribute("allpromotion");//从页面pageContext得到数据  
allpromotion.add("promotioncode=PM120081, description=12, promotionid=100001416");//手动再新增加一条数据

for(int i=0;i<allpromotion.size();i++){ //循环转换
HashMap hm=(HashMap)allpromotion.get(i);
//这里出错,只能成功转换get索引为0的数据,也就是从pageContext的数据能转换成hashmap,而我后添加的却不行,我后添加的数据打印出来的格式是一模一样的。用print出来看两条数据是一样的,为什么他的能转换hashmap我的却不行?
}

------解决方案--------------------
你得到的allpromotion 里面每个元素其实是HashMap对象,所以你之后强转可以,但是你后来添加的仅仅是个String,虽然内容相同,但是结构不同,两个根本就不是一个东西,你强转肯定得不到一个正确的HashMap对象,你直接封装一个内容正确的HashMap add进去即可。
------解决方案--------------------
你弄错了撒 , 你新添的不是Map对象撒,你这个是String肯定报错,还有你直接打印有值map对象看看 ,感觉也会是这样的撒 呵呵 ,面向对象要搞清楚啊,
------解决方案--------------------
探讨
HashMap hm=new HashMap();
hm.put("aaa", "bbb");
Vector v=new Vector();
v.add(hm);
谢谢sffx123,但是vector怎么添加hashMap进去,会报错啊。

------解决方案--------------------
用Vector<HashMap>试试
------解决方案--------------------
探讨
java.util.Vector incompatible with java.util.HashMap

------解决方案--------------------
探讨
JDK1.4悲催啊

------解决方案--------------------
1.4威武。。
------解决方案--------------------
需要new一下在放到Vector中,以前遇到过类似的问题。

Vector allpromotion = (Vector)pageContext.getAttribute("allpromotion");
allpromotion.add(new String("promotioncode=PM120081, description=12, promotionid=100001416"));
------解决方案--------------------
allpromotion.add("promotioncode=PM120081, description=12, promotionid=100001416");
这个添加的是map吗?
表示怀疑啊。这个明显是字符串吧。
String date = "promotioncode=PM120081, description=12, promotionid=100001416";
HashMap hm = new HashMap();
String[] temp = date.split(",");
for(String s:temp){
String[] tempdata = s.split("=");
hm.put(tempdata[0],tempdata[1]);
}