使用POJO在Removeall方法中遇到问题

时间:2014-01-22 12:59:04

标签: java

Java集合

我有两个数据列表,一个是lstreminder,另一个是lstpresent,下面是代码,用于更多规范:

lstintprsnt = new ArrayList<Integer>(); 

lstreminder = DAORemider.getLatestReminder(u.getName());

if (lstpresent == null || lstpresent.isEmpty()) {

            lstpresent = lstreminder;
            System.out.println("i m in if" + lstpresent);
        } else {
            System.out.println("i m in else" + lstpresent.size());
            lstreminder.removeAll(lstpresent);

            /* at this position listreminder will be empty and do not execute loop, 
               but still it hold values and executed loo, but if  I try this with 
               an Integer arraylist then its work fine, means it not executed 
               loop, because it doesnt have any value */

            for (Reminder r : lstreminder) {
                System.out.println("lst" + r.getReminderid());
                lstpresent.add(r);
            }
            System.out.println("i m in else" + lstpresent);

        }
        System.out.println("out    " + lstpresent);
        ServletActionContext.getContext().getSession().put("lstpresent", lstpresent);
        lstpresent.clear();

1 个答案:

答案 0 :(得分:0)

是的,赋值只会将lstreminder(这是一个引用)的值复制到lstpresent。它们都会引用同一个对象。

创建副本非常简单:

List lstpresent = new ArrayList(lstreminder );