我正在尝试根据所有其他值检查 ArrayList 中的所有值,如果它们的值太接近,则删除一个。这是一个例子:
// 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);
}
}
}
寻找最干净和最有效的方法来做到这一点。
[为清楚起见编辑]