繁体   English   中英

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

[英]How to copy the contents of an ArrayList in another 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。 我使用tempRegistry.GetComms()方法创建新的ArrayList TempComms GetComms()是另一个类(类Registry)中的方法,并且仅具有return communications; 命令,通讯是类Registry中的ArrayList phoneNumbers 。( ArrayList phoneNumbers是代码所在类的arrayList。)因此通常使用with

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

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

您正在创建一个新的注册表实例,其中包含一个列表(逗号)。

Registry tempRegistry = new Registry();

然后,您尝试通过调用tempRegistry.GetComms()获得该通讯列表。

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

(因为创建实例tempRegistry之后和调用for循环之前显然不会填充它。

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


    for(Communication comms : TempComms) {

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM