Java 未被替换的对象

Java 未被替换的对象,java,Java,我有两个联系人列表,我想将其中一个替换为另一个: private PhoneCAdapter getContactAdapter(ArrayList<UserAgenda> phoneContacts) throws NetworkConnetionException { List<UserAgenda> serverContacts = WithingsAPI.getInstance().getContactListByType(Common.CONT

我有两个联系人列表,我想将其中一个替换为另一个:

private PhoneCAdapter getContactAdapter(ArrayList<UserAgenda> phoneContacts) throws NetworkConnetionException {     
    List<UserAgenda> serverContacts = WithingsAPI.getInstance().getContactListByType(Common.CONTACT_LIST_TYPE_FILE,"ALL");

    for(UserAgenda pc: phoneContacts){                  
        for(UserAgenda sc : serverContacts){
            if(pc.getEmails() != null){
                ArrayList<String> emailsPc = new ArrayList<String>(pc.getEmails());
                for(String epc: emailsPc){                      
                    ArrayList<String> emailList = new ArrayList<String>(sc.getEmails());                    
                    String emailServer = emailList.get(0);//server contact has only one email                   
                    if(epc.equals(emailServer)){//we have to substitute the object from the server with the phone ones.
                        pc = sc;
                    }
                }
            }
        }
    }   

    PhoneCAdapter ca = new PhoneCAdapter(this, 0, phoneContacts,PhoneContacts.this );
    return ca;
}

如何使用从服务器获得的用户从电话中获取的信息替换对象,而不必为每个字段手动执行此操作?

更改迭代器使用的局部变量不会更改集合的内容。理解修改变量值引用的对象与修改变量之间的区别非常重要。如果修改列表中的引用引用的对象,则该更改将通过列表可见。如果您修改了一个基本上包含该引用副本的变量,那么这根本不会影响列表

我怀疑你想要:

for (int i = 0; i < phoneContacts.size(); i++) {                  
    UserAgenda pc = phoneContacts.get(i);
    for (UserAgenda sc : serverContacts) {
        if (pc.getEmails() != null) {
            ArrayList<String> emailsPc = new ArrayList<String>(pc.getEmails());
            for (String epc: emailsPc) {
                ArrayList<String> emailList = new ArrayList<String>(sc.getEmails());                    
                String emailServer = emailList.get(0);
                if (epc.equals(emailServer)) {
                    pc = sc;
                    // Replace the value in the collection too...
                    phoneContacts.set(i, sc);
                    // Do you want to break here?
                }
            }
        }
    }
}
for(inti=0;i

顺便说一下,不清楚为什么要在循环中创建数组列表。。。(或者为什么每次迭代都要重新创建相同的电子邮件列表)。基本上,代码目前还很不清楚,我强烈怀疑它可以被大大简化——但如果不知道您想要实现什么,就很难做到这一点。

是的,您没有对
电话联系人
进行任何更改。更改局部变量pc的值以引用其他对象不会更改集合…但我可以手动更改它。我想对对象的引用也这样做。不清楚你的意思是什么,但是如果你想让集合引用另一个对象,你需要明确地进行更改。更改局部变量不会导致这种更改。嗨,Jon,我创建ArrayList是因为我使用HashSet存储电子邮件。使用Arraylist对我来说非常方便。@Dayerman:但是你可以像迭代Arraylist一样轻松地迭代HashSet-那么你认为你的
emailsPc
变量实际上提供了什么好处呢?
for (int i = 0; i < phoneContacts.size(); i++) {                  
    UserAgenda pc = phoneContacts.get(i);
    for (UserAgenda sc : serverContacts) {
        if (pc.getEmails() != null) {
            ArrayList<String> emailsPc = new ArrayList<String>(pc.getEmails());
            for (String epc: emailsPc) {
                ArrayList<String> emailList = new ArrayList<String>(sc.getEmails());                    
                String emailServer = emailList.get(0);
                if (epc.equals(emailServer)) {
                    pc = sc;
                    // Replace the value in the collection too...
                    phoneContacts.set(i, sc);
                    // Do you want to break here?
                }
            }
        }
    }
}