charlie mills

Ranch Hand

Posts: 49

posted 8 years ago

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?

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?

posted 8 years ago

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.

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

posted 8 years ago

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 - OCEJPAD 6

How To Ask Questions How To Answer Questions