public static int[] removeElementsGreaterThanThreshold(
ArrayList<Integer> a, int threshold) {
// temporary array of the same size
int[] b = new int[a.size()];
Collections.sort(a);
int bIndex = 0;
// browse 'a' and collect elements that are not greater than the threshold into b
for (int aIndex = 0; aIndex < a.size(); aIndex++) {
if (a.get(aIndex) < threshold) {
b[bIndex] = a.get(aIndex);
bIndex++;
}
}
// copy elements in temporary elements from index 0 to bIndex
int[] result = Arrays.copyOfRange(b, 0, bIndex);
return result;
}
Last edit: Duy Dinh 2013-06-14
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Last edit: Duy Dinh 2013-06-14