title: Genric in Java Error --> class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Comparable;
body: <h2>Question</h2>
  <p>Create a class named KeepSmallArray that uses an array to implement the KeepSmallInterface. Use the program TestKeepSmall to test your implementation.</p>
  <p>Basically i have to drop the two smallest scores from the assignement.</p>
  <h2>Key Points</h2>
  <ol>
  <li>KeepSmallArray object needs to be told at creation time how many grades (or whatever) it should keep track of.</li>
  </ol>
  <h2>Hints</h2>
  <ol>
  <li>helpful if you make the array one space larger than it &quot;needs&quot; to be. For example, if it's remembering 2 grades, make the array size 3.</li>
  <li>You need to keep the elements of the array in order. Consider using the insert algorithm.</li>
  </ol>
  <h2>Error</h2>
  <p><code>Exception in thread &quot;main&quot; java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Comparable; ([Ljava.lang.Object; and [Ljava.lang.Comparable; are in module java.base of loader 'bootstrap')</code></p>
  <h2>Attempted Solution</h2>
  <p>Tried error handling and changed some stuff around but that didnt help
  however the problem seems to be in the constructor of the keepSmallArray class.</p>
  <h2>My code</h2>
  <p><strong>TestKeepSmall</strong></p>
  <pre><code>public class TestKeepSmall {
  
      public static final int NUM_ASGNS = 10;
      public static final Scanner kbd = new Scanner(System.in);
      public static final Random r = new Random();
  
      public static void main(String[] args) {
          for (int numDrops = 2; numDrops &lt; NUM_ASGNS / 2; ++numDrops) {
              int numKeeps = NUM_ASGNS - numDrops;
              Integer[] grades = new Integer[numKeeps];
              int numKept = 0;
  
              System.out.println(&quot;\n&quot;
                      + &quot;Keeping the best &quot; + numKeeps + &quot; of &quot; + NUM_ASGNS
                      + &quot; assignments.&quot;);
  
              KeepSmallInterface&lt;Integer&gt; drops = new KeepSmallArray&lt;&gt;(numDrops);
              // KeepSmallInterface&lt;Integer&gt; drops = new KeepSmallHeap&lt;&gt;(numDrops);
  
              // test size/capacity/isEmpty
              System.out.println(&quot; --&gt; starts with size 0: &quot;
                      + (drops.size() == 0 ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
              System.out.println(&quot; --&gt; starts with given capacity: &quot;
                      + (drops.capacity() == numDrops ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
              System.out.println(&quot; --&gt; starts empty: &quot;
                      + (drops.isEmpty() ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
  
              // toArray
              Object[] dropObjects = drops.toArray();
              System.out.println(&quot; --&gt; toArray() returns correct size: &quot;
                      + (dropObjects.length == drops.size()
                              ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
              Comparable[] dropComps = drops.toArray(new Comparable[3]);
              System.out.println(&quot; --&gt; toArray(T[]) returns correct size: &quot;
                      + (dropComps.length == 3
                              ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
              boolean nulledOut = true;
              for (int i = 0; i &lt; dropComps.length; ++i) {
                  if (dropComps[i] != null) {
                      nulledOut = false;
                  }
              }
              System.out.println(&quot; --&gt; toArray(T[]) nulls unused elements: &quot;
                      + (nulledOut ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
              pause();
  
              // test add
              for (int i = 1; i &lt;= NUM_ASGNS; ++i) {
                  // get a grade from the user
                  int grade = randomGrade();
                  System.out.printf(&quot;A%02d grade is %3d.%n&quot;, i, grade);
  
                  // see if it belongs on the drop list
                  Integer keeper = drops.add(grade);
  
                  // if not, add it to the kept grades array
                  if (keeper != null) {
                      grades[numKept] = keeper;
                      ++numKept;
  
                      // test get
                      Integer newMaxDrop = drops.get(drops.size() - 1);
                      System.out.println(&quot; --&gt; \&quot;bumped out\&quot; largest value: &quot;
                              + (newMaxDrop &lt;= keeper ? &quot;PASS&quot; : &quot;FAIL ***&quot;
                              + &quot;(dropped &quot; + keeper + &quot; instead of &quot; 
                              + newMaxDrop + &quot;)&quot;));
                  }
              }
              pause();
  
              // toArray
              dropObjects = drops.toArray();
              System.out.println(&quot; --&gt; toArray() returns correct size: &quot;
                      + (dropObjects.length == drops.size()
                              ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
              System.out.println(&quot;\n&quot;
                      + &quot;Your dropped grades are &quot; + Arrays.toString(dropObjects)
                      + &quot;\nYour kept grades are &quot; + Arrays.toString(grades));
  
              // toArray(T[])
              dropComps = drops.toArray(new Comparable[3]);
              System.out.println(&quot; --&gt; toArray(T[]) returns correct size: &quot;
                      + (dropComps.length == Math.max(3, drops.size())
                              ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
  
              boolean inOrder = true;
              int upperBound = Math.min(dropComps.length, drops.size());
              for (int j = 1; j &lt; upperBound; ++j) {
                  if (dropComps[j - 1].compareTo(dropComps[j]) &gt; 0) {
                      inOrder = false;
                  }
              }
              System.out.println(&quot; --&gt; toArray(T[]) returns ordered array: &quot;
                      + (inOrder ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
  
              if (upperBound &lt; dropComps.length) {
                  nulledOut = true;
                  for (int i = upperBound; i &lt; dropComps.length; ++i) {
                      if (dropComps[i] != null) {
                          nulledOut = false;
                      }
                  }
                  System.out.println(&quot; --&gt; toArray(T[]) nulls unused elements: &quot;
                          + (nulledOut ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
              }
              
              // contains
              Integer in = oneOf(dropObjects);
              System.out.println(&quot; --&gt; contains &quot; + in + &quot;: &quot;
                      + (drops.contains(in) ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
              Integer out = oneNotOf(dropObjects);
              System.out.println(&quot; --&gt; !contains &quot; + out + &quot;: &quot;
                      + (!drops.contains(out) ? &quot;PASS&quot; : &quot;FAIL ***&quot;));
              pause();
          }
      }
  
      private static void pause() {
          System.out.print(&quot;\n...press enter...&quot;);
          kbd.nextLine();
          System.out.println();
      }
  
      private static int randomGrade() {
          return Math.max(r.nextInt(101), r.nextInt(90));
      }
  
      private static Integer oneOf(Object[] dropComps) {
          int len = dropComps.length;
          int n = r.nextInt(len);
          return (Integer)dropComps[n];
      }
  
      private static Integer oneNotOf(Object[] dropComps) {
          int len = dropComps.length;
          int result = 0;
          boolean ok;
          do {
              ok = true;
              result = r.nextInt(101);
              for (int i = 0; ok &amp;&amp; i &lt; dropComps.length; ++i) {
                  if (dropComps[i].equals(result)) {
                      ok = false;
                  }
              }
          } while (!ok);
          return result;
      }
  
  }
  </code></pre>
  <p><strong>KeepSmallArray</strong></p>
  <pre><code> public class KeepSmallArray&lt;T extends Comparable&lt;? super T&gt;&gt;
      implements KeepSmallInterface&lt;T&gt; {
  
      private T[] smallArray;
  
  
      public KeepSmallArray(int len) {
          if(len &lt;= 0) {
              throw new NullPointerException();
              
          }
          smallArray = (T[]) new Object[len + 1];
          
          
      }
  
      @Override
      public int size() {
          int count = 0;
          for (T item : smallArray) {
              if (item != null) {
                  count++;
              } else {
                  break;
              }
          }
          return count;
      }
  
      @Override
      public int capacity() {
          return smallArray.length;
      }
  
      @Override
      public boolean isEmpty() {
          return size() == 0;
      }
  
      @Override
      public void clear() {
          try{
          smallArray = (T[]) new Object[smallArray.length];
          }
          catch(Exception e){
          }
      }
  
      @Override
      public boolean contains(Object obj) {
          for (T item : smallArray) {
              if (obj.equals(item)) {
                  return true;
              }
          }
          return false;
      }
  
      @Override
      public Object[] toArray() {
          return toArray(smallArray);
      }
  
      @Override
      public Object[] toArray(Object[] array) {
          if (array == null) {
              throw new NullPointerException(&quot;given array is not initialized&quot;);
          }
          return array;
      }
  
      @Override
      public T add(T newElement) {
          if (newElement == null) {
              throw new NullPointerException(&quot;null cannot be added&quot;);
          }
          return null;
      }
  
      @Override
      public T get(int index) {
          if (index &lt; 0 || index &gt; size() - 1) {
              throw new IllegalArgumentException(&quot;index out of range&quot;);
          }
          return smallArray[index];
      }
  }
  </code></pre>
  <p><strong>Keep Interface</strong></p>
  <pre><code>/**
   * A collection of &quot;small&quot; elements, sorted from smallest to largest.
   * The collection contains a limited number of elements (its capacity).
   * The client may request that a new element be added, but that element
   * will only be added if there is room for it &lt;em&gt;or&lt;/em&gt; if it is smaller
   * than one of the elements currently stored in this container.  In the 
   * latter case, the largest element in the container will be &quot;bumped out&quot; 
   * to make room for the new one.
   * &lt;p&gt;
   * Such a container may be used to keep track of assignment grades to be
   * dropped from an average (for example, to track the two smallest of ten
   * assignment grades).  Alternatively, an appropriately programmed class
   * could instead track the eight highest grades from ten (tho' such a class
   * might better be called a &quot;KeepBig&quot; container).  
   * 
   * 
   */
  public interface KeepSmallInterface&lt;T extends Comparable&lt;? super T&gt;&gt; {
  
      /**
       * The number of elements currently in this container.
       *
       * @return the number of elements in this container.
       */
      public int size();
  
      /**
       * The maximum number of elements this container can hold.
       *
       * @return the number of elements this container can hold.
       */
      public int capacity();
  
      /**
       * Whether this bag is empty.
       *
       * @return true if this container has no elements in it; false otherwise.
       */
      public boolean isEmpty();
  
      /**
       * Remove all the elements from this container.
       */
      public void clear();
  
      /**
       * Consider the given element for addition to this container.
       * If there is space available in this container, then given element
       * will be added and &lt;code&gt;null&lt;/code&gt; returned.  Otherwise the
       * largest of the current elements and the given element will be
       * &quot;bumped out&quot; and returned.  (Note that the given element may be
       * the one &quot;bumped out&quot;.)
       *
       * @param newElement the element to add.
       * @return the element &quot;bumped out&quot; of this container;
       *         OR null if no element was &quot;bumped out&quot;.
       * @throws NullPointerException if &lt;code&gt;newElement&lt;/code&gt; is 
       *         &lt;code&gt;null&lt;/code&gt;
       */
      public T add(T newElement);
  
      /**
       * The smallest-but-&lt;tt&gt;i&lt;/tt&gt; element in this container. For example,
       * &lt;code&gt;get(0)&lt;/code&gt; returns the smallest element in this container, 
       * while &lt;code&gt;get(2)&lt;/code&gt; returns the third smallest element 
       * (&lt;i&gt;i.e.&lt;/i&gt; the one with exactly two elements before it in the sorted
       * order).
       *
       * @param index the number of smaller elements than the one requested.
       * @return the smallest-but-&lt;tt&gt;index&lt;/tt&gt; element in this container;
       *         OR null if there is none.
       * @throws IllegalArgumentException if &lt;tt&gt;index&lt;/tt&gt; is not in the range 
       *         &lt;code&gt;0..size()-1&lt;/code&gt;
       */
      public T get(int index);
  
      /**
       * Whether the container contains the given element.
       *
       * @param obj the element to test for.
       * @return true if it's present; false otherwise.
       */
      public boolean contains(Object obj);
  
      /**
       * An array containing all the elements of this container, in order from
       * smallest to largest.
       *
       * @return a sorted array with all this container's elements.
       */
      public Object[] toArray();
  
      /**
       * Returns an array containing all of the elements in this container sorted
       * from smallest to largest; the runtime type of the returned array is that
       * of the given array. If the list fits in the given array, it is returned
       * therein. Otherwise, a new array is allocated with the runtime type of
       * the specified array and just large enuf to hold all this container's
       * elements.
       *
       * @param &lt;E&gt; The base type of the passed-in array.
       * @param array  the array to place the elements in.
       * @return a sorted array with all this container's elements.
       * @throws ArrayStoreException - if the runtime type of the specified 
       * array is not a supertype of the runtime type of every element in this 
       * container.
       * @throws NullPointerException if the specified array is null.
       */
      public &lt;E&gt; E[] toArray(E[] array);
  
  }
  </code></pre>