如何在另一个ArrayList中复制ArrayList的内容

时间:2017-11-19 13:18:45

标签: java arraylist

public boolean isConnectedTo(Suspect aSuspect){

    boolean flag = false;
    Registry tempRegistry = new Registry();
    ArrayList<Communication> TempComms = new ArrayList<Communication>(tempRegistry.GetComms());


    for(Communication comms : TempComms) {
        System.out.println("here");
        for(String PhoneNums : phoneNumbers){
            if(PhoneNums.equals(comms.GetTransmitter())) {
                for(String numbers : aSuspect.getNumbersList()) {
                    if(numbers.equals(comms.GetReceiver())) 
                        flag = true;
                    }

                }
            }
    }
    return flag;

}

所以我试图创建一个程序,除其他外,它将搜索两个ArrayLists(TempComs和phoneNumbers),它将返回true或false,第一个字符串是否与第二个字符串相同或不。我使用方法ArrayList TempComms创建了新的tempRegistry.GetComms()GetComms()是另一个类中的方法(类注册表)并且只有return communications;命令,通信是类注册表中的ArrayList。(ArrayList phoneNumbers是类的数组列表代码进入。)所以通常用

ArrayList<Communication> TempComms = new ArrayList<Communication>(tempRegistry.GetComms());

ArrayList TempComms必须与另一个类中存在的ArrayList communication相同。但我发现由于某些原因问题出现在TempComms中,因为第一个for永远不会运行(因此我使用System.out.println("here");但它从未打印过)。我搜索并尝试了很多,以找到我自己的这个问题的解决方案,但我没有取得一些进展,所以如果有人知道问题在哪里或我做什么,我将不胜感激错了告诉我。不管怎样,谢谢。

1 个答案:

答案 0 :(得分:2)

您正在创建一个包含列表(通信)的注册表的新实例。

Registry tempRegistry = new Registry();

然后,您正试图通过调用tempRegistry.GetComms()来获取该通讯列表。

除非您在构造函数Registry()中填充此通信列表(不仅要实例化,还应添加一些条目), 调用for循环时该列表将为空。

(因为在创建实例 tempRegistry 之后以及在调用for循环之前,您显然 NOT 填充它。

ArrayList<Communication> TempComms = new ArrayList<Communication>(tempRegistry.GetComms());


    for(Communication comms : TempComms) {

因此,TempComms列表也是一个空列表。这就是为什么for循环的内部代码没有执行的原因。

相关问题