This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting ArrayList and printing lowest value" Watch "Sorting ArrayList and printing lowest value" New topic
Author

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
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sorting ArrayList and printing lowest value