wood burning stoves*
The moose likes Beginning Java and the fly likes Sorting ArrayList and printing lowest value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting ArrayList and printing lowest value" Watch "Sorting ArrayList and printing lowest value" New topic

Sorting ArrayList and printing lowest value

charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
I have managed to work out how to print the highest value, but am unsure how to print the lowest one. so far I have this:

I assume that for the lowest value, the idea is the same, but i can't have a certain number in the brackets because the arrayList length is unknown.
Thinking out loud here, but is there anyway I can find out the length of this ArrayList and then put that number in the brackets?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11161

according to the API (which you should learn to read), you can call the size() method on the array to get the number of elements:

int size()
Returns the number of elements in this list.

so, if you have 5 elements, the last will be at index 4. in other words the last index will be at size() - 1.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor

Joined: Oct 27, 2005
Posts: 19654

I suggest you check whether or not the List is empty. If it is, both get(0) and get(temperatures.size() - 1) will fail.

How To Ask Questions How To Answer Questions
charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
OK thanks, i've managed to get it to work
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
also have a look at the reverse(List), reverseOrder(), and reverseOrder(Comparator) methods in the Collections class.
You can combine these with sort to reverse the list.
jQuery in Action, 2nd edition
subject: Sorting ArrayList and printing lowest value
Similar Threads
Problems printing a Treemap 2nd run
Prime Factor Program
compiling help
double value changes
Dependent Combo Box