title: java: convert int to object of type T
body: <p>so I have a class called <strong>ExpandableArrayList</strong> that implements <strong>ListInterface</strong>. This ArrayList is filled with instances of type <strong>Item</strong> (it represents my generic type T). class Item <strong>implements Comparable</strong> , and has the following attributes : String itemNo, String itemName, double price, and int quantity.  </p>
  
  <p>A method called <strong>CheckLimit</strong> In class ExpandableArrayList is supposed to check if any entry in the list is having a quantity below the given limit or not. If it is, it removes it and inserts it at the front of the list.</p>
  
  <p>I already defined <strong>compareTo</strong> for class Item based on item quantity, 
  and this is my current implementation for checklimit : </p>
  
  <pre><code> public void checkLimit (int limit){
  
  /*Type conversions, change limit from int to Object and then to T,   
     in order to use CompareTo: */ 
   Object limitObj = (Integer)limit; 
   T limitT = (T)limitObj; 
   for ( int i=0 ; i&lt; length ; i++ ) { 
      if ( limitT.compareTo(list[i]) &gt; 0){ 
  
          /* ....... Remove and insert at front ......  */
  
      } // end if 
      } // end for   
   } // end checkLimit   
  </code></pre>
  
  <p>It compiles correctly but causes a runtime Exception</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException:
   Item cannot be cast to java.lang.Integer  
  </code></pre>
  
  <p>I then tried to add the following method to class item</p>
  
  <pre><code> /* Added method ConvertToTypeT : 
  this method is called by method checkLimit in class
  ExpandableArrayList. it receives an integer and creates a temporary
  Item Object having this integer as its quantity for comparision purpose only*/ 
  
  public Item convertToTypeT(int limit) { 
    Item converted = new Item (" "," ",0.0,limit);
    return converted;                 } 
  </code></pre>
  
  <p>and changed checklimit to be : </p>
  
  <pre><code> public void checkLimit (int limit){
      for ( int i=0 ; i&lt; length ; i++ ) {
  
      T  limitT =list[i].convertToTypeT(limit);
      if ( limitT.compareTo(list[i]) &gt; 0){ 
  
          /* ....... Remove and insert at front ......  */
  
      } // end if 
      } // end for   
   } // end checkLimit  
  </code></pre>
  
  <p>but did not work even after I changed the <strong>public</strong> Identifier</p>
  
  <pre><code>ExpandableArrayList.java:255: error: cannot find symbol
      T  limitT =list[i].convertToTypeT(limit);  
                        ^
  symbol:   method convertToTypeT(int)
  location: interface Comparable&lt;CAP#1&gt;
  where T is a type-variable:
  T extends Comparable&lt;? super T&gt; declared in class ExpandableArrayList
  where CAP#1 is a fresh type-variable:
  CAP#1 extends Object super: T from capture of ? super T
  </code></pre>
  
  <p>so is there a proper way to perform such a comparison ?  considering that the header of checkLimit was given in the question and it should not be changed (it should always have an int argument ). </p>
  
  <p>many thanks in advance. </p>