This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting ArrayList and printing lowest value

 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you check whether or not the List is empty. If it is, both get(0) and get(temperatures.size() - 1) will fail.
 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK thanks, i've managed to get it to work
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic