There are several things awry here, but let's start with the low-hanging syntax fruit.
The arithmetic operators only work for primitive values, and T
is very much an Object
, so you wouldn't be able to use them here.
Another nuance to your syntax is that you're T
for that static function is not the same as the T
which is bound to your class.
You can fix that, either by adding the bound yourself...
public static <T extends Number> void smallest(ArrayList<T> l)
...but this leads me to ask, why would you want this method to be static at all?
You're obscuring the name of the field l
with the parameter l
, so while you may think that this is going to work for the instance's provided l
, it won't since the method itself is declared static
, and you'd still be shadowing the field's name if it weren't.
What you should do is remove static
from that method, and remove the parameter from the function.
public void smallest() {}
But now, you've got an issue in that you don't actually return what the smallest value is. You should return it instead of nothing:
public T smallest() {}
Getting the last value out of an array is straightforward if you're capable of sorting. You just need to ensure that all of your elements are Comparable
so that they can be sorted, and the values you care about the most - like Integer
, Double
, Float
, Long
- are.
public class MyList<T extends Number & Comparable<T>> {}
If you can sort, then getting the smallest is straightforward:
public T smallest() {
// Don't mutate the order of the original array.
ArrayList<T> listCopy = new ArrayList<>(l);
Collections.sort(listCopy);
return listCopy.get(0);
}
Getting the highest via this approach I leave as an exercise for the reader.