Java list get range

public static int range[ArrayList list]{

You do not need to limit this method to ArrayLists. With minor changes it could accept the more general List, Collection, or even Iterable, which makes your code more reusable [Simon covers Iterable, I will use Collection]. You will still be able to call it with ArrayLists just like before.

I would prefer a more descriptive name than list [perhaps numbers], but the method is small and simple enough that it doesn't really matter. range is perhaps also not the best name, but it seems to be set in stone by the assignment.

Even if the purpose is obvious now, you should document your methods [especially public ones]. Many development tools will be able to automatically fetch and render comments written on the form of Javadoc to help whoever is trying to use this method later on [which, in this case, will probably be you or your instructor, both of whom you want to keep happy].

if[list.size[] == 0]{ return 0; }

Checking your input early is a great idea. Returning 0 may or may not be the correct response to an empty list though - I would be inclined to throw an IllegalArgumentException, but this depends on how the method will be used. We can also use the more readable isEmpty method instead of explicitly comparing the size.

if[list.size[] == 1]{ return 1; }

You do not need to make this check [assuming you perform the correction noted below]. Optimizing for single-element lists is pointless as handling them normally will be more than fast enough anyways.

int firstElement = list.get[1]; int max = firstElement; int min = firstElement;

The first element is accessed with get[0], not get[1]. This doesn't cause any problems at the moment [you could have gotten any element from the list and it would still work], but fixing this allows us to get rid of the special case above. In fact, we do not need to get an element from the list at all, as long as we can be sure that there will be something in the list that is at least as large as max and something that is at least as small as min. We can guarantee this by using Integer.MIN_VALUE and Integer.MAX_VALUE. This lets us support the Collection interface more easily - as there is no direct way to get a particular element from a Collection.

for[int i = 0; i < list.size[]; i++]{ int elementValue = list.get[i];

Since you do not care about the index, and only want the value itself, you can use an "enhanced" for-loop instead. This gives you immediate access to the values instead of having to get them yourself via the index. Other benefits of the enhanced for-loop include better performance with some data structures [like LinkedLists] and compatibility with some data structures which do not use indexes. If you did need to use the index for something, we could not use Collections as they do not necessarily have indexes, and do not guarantee that elements are always in the same order when you iterate over them.

if[max < elementValue]{ max = elementValue; } if[elementValue < min]{ min = elementValue; }

This is fine, except for the indentation. I would personally try to stay consistent with which side of the comparison I put elementValue on, but that's minor.

} return [max - min] + 1; }

Again, fine except for indentation.

I've also gone ahead and inserted some spaces outside of parentheses [except those directly following a method name] because I find that easier to read, but you are free to disagree until you start working for/with someone with a strong opinion on the matter.

With all of that said, here is the code with my updates:

/** * Calculates the range of a collection of numbers. * The range is the largest number in the collection less the smallest number, plus one. * Empty collections have a range of 0. * @param numbers the numbers to scan through * @return the range of the collection */ public static int range[Collection numbers] { if [numbers.isEmpty[]] { return 0; } int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for [Integer number : numbers] { if [number > max] { max = number; } if [number < min] { min = number; } } return [max - min] + 1; }

Video liên quan

Chủ Đề