[英]Error declaring an ArrayList of byte[] array copy returned deceptive error message saying to use iterator
I am trying to copy an ArrayList of byte[] array within a builder constructor.我正在尝试在构建器构造函数中复制 byte[] 数组的 ArrayList。 I want to copy arrayLst1 to arrayLst2.
我想将 arrayLst1 复制到 arrayLst2。 I tried doing it with this code:
我试着用这个代码来做:
builder.arrayLst2.forEach(item->this.arrayLst1.add(item));
but I got the following error:但我收到以下错误:
Exception in thread "main" java.util.ConcurrentModificationException
So Google lead me to Iterators here , here and here so I tried this code (and other variations).所以谷歌把我带到了 Iterators here 、 here和here ,所以我尝试了这段代码(和其他变体)。
for (Iterator itr = builder.arrayLst1.iterator(); itr.hasNext();) {
this.arrayLst2.add(itr.next());
}
The second line gives the error message "no suitable method found for add method."第二行给出错误消息“没有找到适合添加方法的方法”。
I have tried along the lines of:我已经尝试过:
Iterator itr = this.arrayLst.iterator();
builder.arrayLst2.forEach(item->itr.next() );
but I can't figure out how to step through the first array1 and use iterator to add/copy to array2.但我不知道如何单步执行第一个 array1 并使用迭代器添加/复制到 array2。
How do I clone one ArrayList byte[] to another?如何将一个 ArrayList byte[] 克隆到另一个? I am sure there must be an easy solution but I can't find it.
我相信一定有一个简单的解决方案,但我找不到。
Edit: The declaration error effectively meant that the arraylist wasn't declared.编辑:声明错误实际上意味着未声明数组列表。 Rather than say this, the compiler error message pointed to needing to use an iterator.
编译器错误消息指出需要使用迭代器,而不是这样说。
this.arrayLst1 = new ArrayList<>(builder.arrayLst2);
or或者
this.arrayLst1 = new ArrayList<>();
this.arrayLst1.addAll(builder.arrayLst2);
Credit to this answer belongs to #Zaki Anwar Hamdani.这个答案归功于#Zaki Anwar Hamdani。 I made an error in the declaration.
我在声明中犯了一个错误。
The following declaration works.以下声明有效。
this.flagImgs = new ArrayList <>( this.flagImageFilenames.size());
builder.flagImgs.forEach(item->this.flagImgs.add(item));
The thing that stumped me was that the error messages did not hint at a problem with the declaration.难倒我的是错误消息并没有暗示声明有问题。 The reference to Iterators sent me on a wild goose chase to nowhere.
对 Iterators 的引用让我无所适从。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.