File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Getting highest ArrayListValue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting highest ArrayListValue" Watch "Getting highest ArrayListValue" New topic
Author

Getting highest ArrayListValue

joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
Hello,

i have made an ArrayList (ie ArrayList one = new ArrayList() ). This list has 1 - 100 values (1 - 900) added to it. It works fine. How do i get the highest value from the array? I have been looking at the javasun basic tutorials and examples and cannot find what i need.

thankyou,

Joseph.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Well, there are two ways you could do it. Either sort the list (using Collections.sort()) and then take the value at the highest index, or scan through the list, looking at each item, remembering the largest item seen so far. The former is probably easier. The latter has better performance.


[Jess in Action][AskingGoodQuestions]
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello and thankyou for your reply.

if you suggest collections, thats what ill research into next. i guess collections sort the highest value into ArrayList(0). Now that i know where to look, i should get this sorted soon

thankyou,

Joseph
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
Hello,

when i use Collection.sort(ArrayList) and get the results, it sorts in a non numerical order. instead of lowest first and highest last, it goes by first digits, so if there is 1, 2, 3, 121, 234 it sorts it like this:
1, 121, 2, 234, 3.

any ideas?

joseph
[ March 02, 2005: Message edited by: joseph mcgratton ]
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
What type of objects are in your collection? Are they Strings? If so, then that's why they sort that way. Put Integer objects into the array and it will work almost as you expected (the sort order will be lowest to highest).


James Carman, President<br />Carman Consulting, Inc.
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello,

thankyou james for your help. it could be because they are strings. I cant remember why, but something would work with integers so i switch between strings and integers. now i know where the problem is, ill be ok.

thanks again

joseph
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting highest ArrayListValue