This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find min and max in ArrayList

 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't want to use Collections.min(), do you ?
 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, I don't think so.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that your array contains Double objects and that the array is not empty:

 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it because the array is empty ? Or because it does not contains Doubles ?
Can you post the exception ?
 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You told that the array contains Double objects, but it contains Strings !
You need to convert the strings first.

 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have fun with the max and avg, you should figure it out yourself
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic