Hey, I'm writing a program that is using an arraylist.
I've used Collections.max(arraylist) and Collections.min(arraylist) to return the longest and shortest elements in the arraylist. However, the shortest an element in the arraylist can be is 1, because otherwise the element is empty and is skipped when being counted.
How can I get my program to include the empty elements when searching for the shortest element?
thanks for your help, I know it's probably a simple answer and only one or two lines of code but I'm struggling to think of it
I need more detail. What is your arraylist holding? Also, an arraylist readjusts the size when you add or remove something. So there shouldn't be empty elements.
There are 2 max and 2 min methods. The second version of each takes a Comparator as a parameter. Therefore write your own Comparator to compare the elements however you like.
Ahh thanks for your help, was just an error on my part. Collections.min already does what I wanted it to, I just called max instead of min in one part.