huizai521 2010-04-16 16:53
浏览 1102
已采纳

如何获取MAP 中对象中的值

这个是个VO对像
[quote="java"]

package com.lihui.VO;

public class AddListVO {
private int goodId;
private int buyNum;
private int orderId;
public AddListVO() {
super();
// TODO Auto-generated constructor stub
}
public AddListVO(int goodId, int buyNum, int orderId) {
super();
this.goodId = goodId;
this.buyNum = buyNum;
this.orderId = orderId;
}
public int getGoodId() {
return goodId;
}
public void setGoodId(int goodId) {
this.goodId = goodId;
}
public int getBuyNum() {
return buyNum;
}
public void setBuyNum(int buyNum) {
this.buyNum = buyNum;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}

}
[/quote]

然后是主方法:
[quote="java"]

public class map {
private AddListVO addListVO;

public static void main(String args[]) 
{ 
AddListVO listVO=new AddListVO(1, 2, 3);
HashMap hashmap = new HashMap(); 
hashmap.put("Item0", listVO); 
hashmap.put("Item1", listVO); 
hashmap.put("Item2", listVO); 
hashmap.put("Item3", listVO); 
Set set = hashmap.entrySet(); 
Iterator iterator = set.iterator(); 
while (iterator.hasNext()){ 
Map.Entry mapentry = (Map.Entry) iterator.next(); 
System.out.println(mapentry.getKey() + "/" + mapentry.getValue()); 
} 
}
public AddListVO getAddListVO() {
    return addListVO;
}
public void setAddListVO(AddListVO addListVO) {
    this.addListVO = addListVO;
} 

}

[/quote]

我得到的是
Item1/com.lihui.VO.AddListVO@c17164
Item2/com.lihui.VO.AddListVO@c17164
Item0/com.lihui.VO.AddListVO@c17164
Item3/com.lihui.VO.AddListVO@c17164

我想得到这个对象里的元素的值 应该咋办?

  • 写回答

2条回答 默认 最新

  • 拽拽的初行者 2010-04-16 17:01
    关注

    [color=blue][b]mapentry.getKey() 获得的是 item1等key

    而:
    mapentry.getValue()); 获得的是你放入的listVO这个对象。

    当你用System.out.println();输出对象时,输出的是该对象的toString方法的返回值。

    而你的AddListVO没有复写toString 方法,所以输出的就是com.lihui.VO.AddListVO@c17164 。[/b][/color]

    [b]你可以这样:[/b]

    code="java"mapentry.getValue()).getGoodId();[/code]

    [b]也可以在AddListVO中Override复写 toString 方法[/b]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘