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; }
Log in to post a comment.
Last edit: Duy Dinh 2013-06-14