Java 声明byte[]数组副本的ArrayList时出错,返回欺骗性错误消息,表示要使用迭代器

Java 声明byte[]数组副本的ArrayList时出错,返回欺骗性错误消息,表示要使用迭代器,java,arraylist,iterator,clone,Java,Arraylist,Iterator,Clone,我正在尝试在生成器构造函数中复制byte[]数组的ArrayList。我想把arrayLst1复制到arrayLst2。我尝试使用以下代码执行此操作: builder.arrayLst2.forEach(item->this.arrayLst1.add(item)); 但我得到了以下错误: Exception in thread "main" java.util.ConcurrentModificationException 所以Google让我找到了迭代器,所以我尝试了这段代码(以

我正在尝试在生成器构造函数中复制byte[]数组的ArrayList。我想把arrayLst1复制到arrayLst2。我尝试使用以下代码执行此操作:

builder.arrayLst2.forEach(item->this.arrayLst1.add(item)); 
但我得到了以下错误:

Exception in thread "main" java.util.ConcurrentModificationException
所以Google让我找到了迭代器,所以我尝试了这段代码(以及其他变体)

第二行给出错误消息“找不到适合add方法的方法”

我尝试了以下几点:

Iterator itr = this.arrayLst.iterator();        
builder.arrayLst2.forEach(item->itr.next()  );
但我不知道如何单步执行第一个数组1并使用迭代器向数组2添加/复制

如何将一个ArrayList字节[]克隆到另一个?我相信一定有一个简单的解决办法,但我找不到

编辑:声明错误实际上意味着没有声明arraylist。编译器错误消息指出需要使用迭代器,而不是这样说。

this.arrayLst1=new ArrayList(builder.arrayLst2);
this.arrayLst1 = new ArrayList<>(builder.arrayLst2);

this.arrayLst1=new ArrayList();
this.arrayLst1.addAll(builder.arrayLst2);

这个答案的功劳属于扎基·安瓦尔·哈姆达尼。 我在声明中犯了一个错误

以下声明有效

        this.flagImgs = new ArrayList <>( this.flagImageFilenames.size());
        builder.flagImgs.forEach(item->this.flagImgs.add(item)); 
this.flagImgs=newArrayList(this.flagImageFilenames.size());
builder.flagImgs.forEach(item->this.flagImgs.add(item));

让我感到困惑的是,错误消息并没有暗示声明有问题。对迭代器的引用让我陷入了白费力气。

你能添加arrayLst1和arrayLst2的声明吗?嗨。我已经声明了这两个变量,只是没有在代码段中显示。这就是为什么要求您在代码段中显示它们。张贴一个完整的例子。在发布的代码中,到处都在使用原始类型。不要那样做。泛型自Java5以来就存在。我们在Java9上,我仍然处于学习曲线的底部,这包括知道在这个论坛上问什么和如何提问。我真的很感激人们自愿帮助像我这样无助的人。我边走边学。如果直到Java5才添加泛型,我可能还不需要了解它们。现在我要做的就是学习如何克隆阵列。这在任何语言中都应该是简单的。声明如下:
this.arrayLst1 = new ArrayList<>();
this.arrayLst1.addAll(builder.arrayLst2);
        this.flagImgs = new ArrayList <>( this.flagImageFilenames.size());
        builder.flagImgs.forEach(item->this.flagImgs.add(item));