Java List的奇怪行为

时间:2014-01-26 05:11:46

标签: java java-ee gwt

我有这个要求用千分隔符显示金额。我有这两个方法的POJO类,我根据我的要求调用

public Integer getTotalAmount() {
    return totalAmount;
}

public String getTotalAmountWithSeparator() {
    return String.format("%,d", totalAmount);
}

public void setTotalAmount(Integer totalAmount) {
    this.totalAmount = totalAmount;
}

现在当我在我的另一个类中使用此方法 getTotalAmountWithSeparator()时,我会执行println以查看该数量是否正确显示(它确实如此)。

List<SupplierOrderDetails> list = SupplierOrderDetailBussinessLogic.getInstance().getSupplierOrderDetailsFromsupplierOrder(supplierOrder);

      DataProviderBuilder dpb = new DataProviderBuilder();

    // add heading data
    dpb.add("so", supplierOrder.getSupplierOrderNo());
    dpb.add("sn", supplierOrder.getSupplier().getPerName());
    dpb.add("sec", supplierOrder.getSection().getAlternateName());
    dpb.add("od", supplierOrder.getSupplierOrderCreated().toString());

    // add table data
    dpb.addJavaObject(list, "data");

这是实际的方法 getSupplierOrderDetailsFromsupplierOrder(supplierOrder); 从数据库中获取数据。

@SuppressWarnings("unchecked")
public List<SupplierOrderDetails> getSupplierOrderDetailsFromsupplierOrder(SupplierOrder supplierOrderDetails){
    Session hibernateSession = HibernateUtills.getInstance().getHibernateSession();
    Criteria criteria = hibernateSession.createCriteria(SupplierOrderDetails.class);
    criteria.add(Restrictions.eq("supplierOrderID", supplierOrderDetails));
    List<SupplierOrderDetails> models = criteria.list();

    System.out.println(" models.size()     " + models.size());

    for (int i = 0; i < models.size(); i++)
    {
        if (models.get(i).getId() != null)
        {
            models.get(i).getProductID().getProductCode();
            models.get(i).getProductID().getBrandName();
            models.get(i).getPurchasePrice();
            models.get(i).getOrderQty();
            models.get(i).getTotalAmountWithSeparator();
            System.out.println(models.get(i).getProductID().getBrandName() + "  TotalAmount      " + models.get(i).getTotalAmountWithSeparator());

        }
        // System.out.println(models.get(i).getPurchasePrice());
    }
    return models;
} 

但是当我在这里打印列表数据时,它没有显示数量的分隔符为什么?????我做错了什么

dp = getSupplierOrderData(Long.parseLong(supplierOrderId));
System.out.println("DATA    "+dp.getString("data"));

1 个答案:

答案 0 :(得分:2)

因为dp.toString()方法使用getTotalAmount()而不是getTotalAmountWithSeparator()