2

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 :/

missrg
  • 585
  • 2
  • 8
  • 19

3 Answers3

1

I found it guys! The problem was that instead of storing the result of the function in a float value and then using it, I called the function everytime I wanted the value in order to save memory space. I had forgotten to initialize one value of the function though, so everytime it gave me a different start-value, so everytime I called the function it returned a different result..

Thank you very much for your help, it may have taken me much longer to find it without your questions which led my thought to the real problem :)

missrg
  • 585
  • 2
  • 8
  • 19
0

IndexOf returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element, and NOT the maximum you're looking for.

check documentation here http://download.java.net/jdk7/archive/b123/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)

You also have to specify the type of the elements stored on the ArrayList. Try :

... new ArrayList<Float>() ; .. 
  • As a matter of fact it should return the index of the `max_element` which was found with `Collection.max()` – svz Nov 23 '12 at 12:27
  • @alji-mohamed Yes, but `Collections.max()` does (or at least should) return the maximum, depending on the `Comparable` implementation. – Andy Nov 23 '12 at 12:29
  • I think he defines ArrayList< **Float** >() with int rather than Float. please check ! –  Nov 23 '12 at 12:31
  • @ALJIMohamed what do you mean with "_ArrayList< Float >() with int_"? – Andy Nov 23 '12 at 12:37
  • `ArrayList()` that means the ArrayList is supposed to store ints. If you put Floats on it, it will be rounded down (or up) ! –  Nov 23 '12 at 12:51
0

Hope you have both the following libraries added...

import java.util.ArrayList;
import java.util.Collections;

can you try this code? Instead of saving to a Float object,

Object obj = Collections.max(arrayList);
bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • Similar [question](http://stackoverflow.com/questions/8304767/how-to-get-maximum-value-from-the-list-arraylist) – bonCodigo Nov 23 '12 at 12:31
  • Yes, I do have the libraries added. I tried to change it to Object but still didn't worked properly :S – missrg Nov 23 '12 at 12:34