• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Getting highest ArrayListValue

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic