ArrayList 内のすべての値を他のすべての値と照合して、値が近すぎる場合は 1 つ削除しようとしています。次に例を示します。
// make an ArrayList of random numbers
ArrayList<Integer> nums = new ArrayList<Integer>();
for (int i=0; i<25; i++) {
int rand = int(random(255));
nums.add(rand);
println(rand);
}
// go through all numbers and compare
// (loop backwards to prevent ConcurrentModificationException)
for (int i = nums.size()-1; i >= 0; i--) {
int current = nums.get(i);
println("Current #: " + current);
// how to do this?
// not sure if there's a faster way that
// would avoid running through the entire
// ArrayList for every element...
for (Integer other : nums) {
if (abs(current - other) < 5) {
nums.remove(current);
}
}
}
これを行うための最もクリーンで最も効率的な方法を探しています。
[明確にするために編集]