patternjavaModerate
Calculate range of ArrayList
Viewed 0 times
rangearraylistcalculate
Problem
Write a method range that accepts an ArrayList of integers as a parameter and that returns the range of values contained in the list, which is defined as 1 more than the difference between the largest and smallest elements.
I feel like I'm beginning to understand conditionals, but for some reason this just makes me cringe. Is it good practice to have these conditionals as catch-alls in the beginning? Should I be using
I feel like I'm beginning to understand conditionals, but for some reason this just makes me cringe. Is it good practice to have these conditionals as catch-alls in the beginning? Should I be using
else ifs instead?public static int range(ArrayList list){
if(list.size() == 0){
return 0;
}
if(list.size() == 1){
return 1;
}
int firstElement = list.get(1);
int max = firstElement;
int min = firstElement;
for(int i = 0; i < list.size(); i++){
int elementValue = list.get(i);
if(max < elementValue){
max = elementValue;
}
if(elementValue < min){
min = elementValue;
}
}
return (max - min) + 1;
}Solution
Use this:
Only being able to do the job on
No, it is no need to use
Rewriting with
You currently check the first item in the list twice. If your code would accept an
The only thing you need is the
Happy coding!
if (list.size() <= 1) {
return list.size();
}list.get(1) refers to the second element in the list, not the first. Either rename the variable or use list.get(0). With that change, you don't need to use a list size of 1 as a special case.Only being able to do the job on
ArrayList is quite useless. Your method can take an argument of type List instead.No, it is no need to use
else if in this case.Rewriting with
IterableYou currently check the first item in the list twice. If your code would accept an
Iterable, you would support even more data types (You still support ArrayList also).The only thing you need is the
Iterator from the iterable. When you have that, you can first see if it contains at least one item. If it doesn't, return 0. Otherwise, you do similar to what you are already doing, but with different code.public static int range(Iterable numbers) {
Iterator iterator = numbers.iterator();
if (!iterator.hasNext()) {
return 0;
}
int firstElement = iterator.next();
int max = firstElement;
int min = firstElement;
while (iterator.hasNext()) {
int elementValue = iterator.next(i);
if (max < elementValue) {
max = elementValue;
}
if (elementValue < min) {
min = elementValue;
}
}
return (max - min) + 1;
}Happy coding!
Code Snippets
if (list.size() <= 1) {
return list.size();
}public static int range(Iterable<Integer> numbers) {
Iterator<Integer> iterator = numbers.iterator();
if (!iterator.hasNext()) {
return 0;
}
int firstElement = iterator.next();
int max = firstElement;
int min = firstElement;
while (iterator.hasNext()) {
int elementValue = iterator.next(i);
if (max < elementValue) {
max = elementValue;
}
if (elementValue < min) {
min = elementValue;
}
}
return (max - min) + 1;
}Context
StackExchange Code Review Q#114194, answer score: 11
Revisions (0)
No revisions yet.