My answer is almost the same as the correct answer, except that in the removeAllNumbersGreaterThan10 method, I did not create a new list. Why am I getting an error message stating that I need to make the removeAllNumbersGreaterThan10 method work for collections of arbitrary length?
評論 (4)
- 熱門
- 最新
- 最舊
您必須 登入 才能留下評論
Thomas
8 七月 2023, 12:09
without knowing the code in question I just can ask my crystal ball: murmor, murmor... no, it stays foggy
0
为中华崛起而读书丶
10 七月 2023, 04:58
public static HashSet<Integer> removeAllNumbersGreaterThan10(HashSet<Integer> set) {
// write your code here
for(Integer n : set){
if(n > 10){
set.remove(n);
}
}
return set;} I apologize, but I forgot to copy my code. This is my original code, and the last requirement didn't pass.
+1
Thomas
10 七月 2023, 05:48solution
Do not write code blindly, always test it. For that you can use the main method.
I admit I did not do here either ;) but I'm sure your code will throw a ConcurrentModificationException and that will prohibit it from validating.
Reason: When using an Iterator to walk over a collection you may only use the Iterators functionality to modify that collection. A for-each loop internally uses an Iterator but you used the collections methods to modify it (set.remove()) -> ConcurrentModificationException
Possible test code:
+2
为中华崛起而读书丶
10 七月 2023, 07:37
Thank you, I understand now.
+1