title: Best way to implement generics in methods?
body: <p>I'm having issues with a programming exercise because the book I'm reading only showed how to use print generic methods. Which is easy. But the exercise asks to find the the largest and smallest value of an ArrayList. I figured out a way to do it, shown in my code, but I'm just thinking to myself what's the point of using generics in the first place?</p>
  <p>Here are the exercise instructions:</p>
  <p>Write a generic class named MyList, with a type parameter T. The type parameter T should be
  constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T.
  Write a public method named add, which accepts a parameter of type T. When an argument is passed
  to the method, it is added to the ArrayList. Write two other methods, largest and smallest, which
  return the largest and smallest values in the ArrayList.</p>
  <p>My code:</p>
  <pre><code>//Main
  
  import java.io.*;
  import java.util.*; 
  
  public class Main {
  
      static PrintWriter writer = new PrintWriter(System.out, true);
  
      public static void main(String[] args) throws IOException {
  
          ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
  
          MyList&lt;Integer&gt; myList = new MyList&lt;&gt;(list);
  
          myList.add(3);
          myList.add(5);
          myList.add(44);
          myList.add(654);
  
          int first = myList.getArrayList().get(0);
          int largest = 0;
          int smallest = 0;
  
          for (int i = 1; i &lt; list.size(); i++) {
              largest = myList.getLargest(first, myList.getArrayList().get(i));
              smallest = myList.getSmallest(first, myList.getArrayList().get(i));
          }
  
          System.out.println(&quot;Largest: &quot; + largest + &quot;\nSmallest: &quot; + smallest);
  
      }
  }
  
  //MyList Class
  
  import java.util.ArrayList;
  
  public class MyList &lt;T extends Number&gt; {
      private ArrayList&lt;T&gt; arrayList;
  
      public MyList(ArrayList&lt;T&gt; arrayList) {
          this.arrayList = arrayList;
      }
  
      public void add(T item) {
          arrayList.add(item);
      }
  
      public ArrayList&lt;T&gt; getArrayList() {
          return arrayList;
      }
  
      public int getLargest(int x, int y) {
          return Math.max(x, y);
      }
  
      public int getSmallest(int x, int y) {
          return Math.min(x, y);
      }
  }
  </code></pre>
  <p>If I try writing the methods with T types I get errors when using operators like &gt; or + and Math.max() also won't work. I understand why now, I just don't understand what type am I suppose to return and input for the methods. int, Integer, or T? And if I wanted to use a an ArrayList using doubles what then so the methods can work with both types?
  Any input as to what would be the best way to solve this problem using generics would be greatly appreciated. Thank you.</p>