I have an ArrayList filled with Double values, and based on user input (an int), I need to find the indices of the entered amount of smallest Doubles from this List. For example, if the user were to type in 5, and the ArrayList looked as such (is actually much bigger):
-6789.44658
-27239.73827
-12365.78370
-456.789457
-4768.42579
-15263.26399
-15263.26399
-0.0
-24688.7289
I would get 1, 5, 8, 2, 6(order of the 5 smallest Doubles in terms of least to greatest doesn't matter). Here's what I have so far:
int[] indices = new int[input];
List<Double> copy = new ArrayList<Double>(origList); //origList is list of Doubles
for (int t = 0; t < input; t++)
{
indices[t] = origList.indexOf(Collections.min(copy));
copy.remove(Collections.min(copy));
}
There are 2 problems with this though:
- it's really inefficient
- in the example ArrayList given above, two of the values are the
same (there could even be three values that are the same). If
identical values are the lowest values in copy, because indexOf()
returns the index of the first occurrence of this value in
origList,the same index is returned twice. But, none of the indices can be the same.
Thanks for any help!