I have an ArrayList with Float elements in Java, and I am trying to find the position of the element with the maximum value. I tried to do this with the following code but it didn't work:
Float obj = Collections.max(arraylist);
int index=arraylist.indexOf(obj);
The problem is that Collections.max()
, which seems to work for int values, returns a wrong result in my case..
Any ideas on what is going wrong and how can I do this differently?
Correction: The problem is not where I thought, it is when adding the elements to the Arraylist. The values that I add are the float results of a function. I print the result of the function right before I add it, then I use the usual arraylist.add(value)
code, but when I ask to print the value of the arraylist right after its addition, a completely different one is returned :/