This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Find min and max in ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Find min and max in ArrayList" Watch "Find min and max in ArrayList" New topic
Author

Find min and max in ArrayList

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hi, I am having a little trouble finding out why my findMin method (which will be just the oposite of my findMax method is returning some funky number that isn't even in the array.

The method I have to find a min element in an ArrayList is:



Any suggestions on how to clean this up would be greatly appreciated.


Thanks in advance.
[ April 02, 2006: Message edited by: Gabriel White ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You don't want to use Collections.min(), do you ?


[My Blog]
All roads lead to JavaRanch
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
no, I don't think so.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Assuming that your array contains Double objects and that the array is not empty:

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hi Satou, and thanks for the help on this, but your code is throwing an exception when you try to cast to Double object.




And yes, my array is full of doubles and is not empty.

Thanks again for your help.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Is it because the array is empty ? Or because it does not contains Doubles ?
Can you post the exception ?
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
It's from Jbuilder, but line 127 is that line that I indicated earlier.

I can do a SOP on the array being passed and it shows all 9 numbers from a file.

[11.3, 4.23, .00034, 123456.78, 7.12, 11.4, 95, 17, -34.567]




Thanks again for your help
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hi Satou, I added these lines in and they output the following, just showing that an array is being passed successfully.

Thanks

System.out.println(array);
System.out.println(array.get(0));

Output:

[11.3, 4.23, .00034, 123456.78, 7.12, 11.4, 95, 17, -34.567]
11.3
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You told that the array contains Double objects, but it contains Strings !
You need to convert the strings first.

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
You da man Satou.

That did it. I will have to work on some max and some average methods, but this one was sweating me.

Thanks bro.

Gabe.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Have fun with the max and avg, you should figure it out yourself
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find min and max in ArrayList
 
Similar Threads
[newbie] Cannot access StaticInnerClass
Comparing ArrayList and double array[]
Array i/o and sorting
Returning only minimum value...
Getting value of all the indices of all objects from an arraylist