• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting highest ArrayListValue

 
joseph mcgratton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
joseph mcgratton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
joseph mcgratton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic