【问题标题】:Genric in Java Error --> class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Comparable;Java 错误中的泛型 --> 类 [Ljava.lang.Object;不能转换为类 [Ljava.lang.Comparable;
【发布时间】:2022-08-09 13:02:59
【问题描述】:

问题

创建一个名为 KeepSmallArray 的类,该类使用数组来实现 KeepSmallInterface。使用程序 TestKeepSmall 来测试你的实现。

基本上我必须从作业中删除两个最小的分数。

关键点

  1. KeepSmallArray 对象需要在创建时被告知它应该跟踪多少等级(或其他)。

    提示

    1. 如果您使数组比它“需要”大一个空间,这很有帮助。例如,如果它记住 2 个成绩,则将数组大小设为 3。
    2. 您需要保持数组元素的顺序。考虑使用插入算法。

      错误

      Exception in thread \"main\" 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\')

      尝试的解决方案

      尝试了错误处理并改变了一些东西,但这没有帮助 但是问题似乎出在 keepSmallArray 类的构造函数中。

      我的代码

      TestKeepSmall

      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 < NUM_ASGNS / 2; ++numDrops) {
                  int numKeeps = NUM_ASGNS - numDrops;
                  Integer[] grades = new Integer[numKeeps];
                  int numKept = 0;
      
                  System.out.println(\"\\n\"
                          + \"Keeping the best \" + numKeeps + \" of \" + NUM_ASGNS
                          + \" assignments.\");
      
                  KeepSmallInterface<Integer> drops = new KeepSmallArray<>(numDrops);
                  // KeepSmallInterface<Integer> drops = new KeepSmallHeap<>(numDrops);
      
                  // test size/capacity/isEmpty
                  System.out.println(\" --> starts with size 0: \"
                          + (drops.size() == 0 ? \"PASS\" : \"FAIL ***\"));
                  System.out.println(\" --> starts with given capacity: \"
                          + (drops.capacity() == numDrops ? \"PASS\" : \"FAIL ***\"));
                  System.out.println(\" --> starts empty: \"
                          + (drops.isEmpty() ? \"PASS\" : \"FAIL ***\"));
      
                  // toArray
                  Object[] dropObjects = drops.toArray();
                  System.out.println(\" --> toArray() returns correct size: \"
                          + (dropObjects.length == drops.size()
                                  ? \"PASS\" : \"FAIL ***\"));
                  Comparable[] dropComps = drops.toArray(new Comparable[3]);
                  System.out.println(\" --> toArray(T[]) returns correct size: \"
                          + (dropComps.length == 3
                                  ? \"PASS\" : \"FAIL ***\"));
                  boolean nulledOut = true;
                  for (int i = 0; i < dropComps.length; ++i) {
                      if (dropComps[i] != null) {
                          nulledOut = false;
                      }
                  }
                  System.out.println(\" --> toArray(T[]) nulls unused elements: \"
                          + (nulledOut ? \"PASS\" : \"FAIL ***\"));
                  pause();
      
                  // test add
                  for (int i = 1; i <= NUM_ASGNS; ++i) {
                      // get a grade from the user
                      int grade = randomGrade();
                      System.out.printf(\"A%02d grade is %3d.%n\", 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(\" --> \\\"bumped out\\\" largest value: \"
                                  + (newMaxDrop <= keeper ? \"PASS\" : \"FAIL ***\"
                                  + \"(dropped \" + keeper + \" instead of \" 
                                  + newMaxDrop + \")\"));
                      }
                  }
                  pause();
      
                  // toArray
                  dropObjects = drops.toArray();
                  System.out.println(\" --> toArray() returns correct size: \"
                          + (dropObjects.length == drops.size()
                                  ? \"PASS\" : \"FAIL ***\"));
                  System.out.println(\"\\n\"
                          + \"Your dropped grades are \" + Arrays.toString(dropObjects)
                          + \"\\nYour kept grades are \" + Arrays.toString(grades));
      
                  // toArray(T[])
                  dropComps = drops.toArray(new Comparable[3]);
                  System.out.println(\" --> toArray(T[]) returns correct size: \"
                          + (dropComps.length == Math.max(3, drops.size())
                                  ? \"PASS\" : \"FAIL ***\"));
      
                  boolean inOrder = true;
                  int upperBound = Math.min(dropComps.length, drops.size());
                  for (int j = 1; j < upperBound; ++j) {
                      if (dropComps[j - 1].compareTo(dropComps[j]) > 0) {
                          inOrder = false;
                      }
                  }
                  System.out.println(\" --> toArray(T[]) returns ordered array: \"
                          + (inOrder ? \"PASS\" : \"FAIL ***\"));
      
                  if (upperBound < dropComps.length) {
                      nulledOut = true;
                      for (int i = upperBound; i < dropComps.length; ++i) {
                          if (dropComps[i] != null) {
                              nulledOut = false;
                          }
                      }
                      System.out.println(\" --> toArray(T[]) nulls unused elements: \"
                              + (nulledOut ? \"PASS\" : \"FAIL ***\"));
                  }
                  
                  // contains
                  Integer in = oneOf(dropObjects);
                  System.out.println(\" --> contains \" + in + \": \"
                          + (drops.contains(in) ? \"PASS\" : \"FAIL ***\"));
                  Integer out = oneNotOf(dropObjects);
                  System.out.println(\" --> !contains \" + out + \": \"
                          + (!drops.contains(out) ? \"PASS\" : \"FAIL ***\"));
                  pause();
              }
          }
      
          private static void pause() {
              System.out.print(\"\\n...press enter...\");
              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 && i < dropComps.length; ++i) {
                      if (dropComps[i].equals(result)) {
                          ok = false;
                      }
                  }
              } while (!ok);
              return result;
          }
      
      }
      

      保持小数组

       public class KeepSmallArray<T extends Comparable<? super T>>
          implements KeepSmallInterface<T> {
      
          private T[] smallArray;
      
      
          public KeepSmallArray(int len) {
              if(len <= 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(\"given array is not initialized\");
              }
              return array;
          }
      
          @Override
          public T add(T newElement) {
              if (newElement == null) {
                  throw new NullPointerException(\"null cannot be added\");
              }
              return null;
          }
      
          @Override
          public T get(int index) {
              if (index < 0 || index > size() - 1) {
                  throw new IllegalArgumentException(\"index out of range\");
              }
              return smallArray[index];
          }
      }
      

      保留界面

      /**
       * A collection of \"small\" 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 <em>or</em> 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 \"bumped out\" 
       * to make room for the new one.
       * <p>
       * 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 \"KeepBig\" container).  
       * 
       * 
       */
      public interface KeepSmallInterface<T extends Comparable<? super T>> {
      
          /**
           * 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 <code>null</code> returned.  Otherwise the
           * largest of the current elements and the given element will be
           * \"bumped out\" and returned.  (Note that the given element may be
           * the one \"bumped out\".)
           *
           * @param newElement the element to add.
           * @return the element \"bumped out\" of this container;
           *         OR null if no element was \"bumped out\".
           * @throws NullPointerException if <code>newElement</code> is 
           *         <code>null</code>
           */
          public T add(T newElement);
      
          /**
           * The smallest-but-<tt>i</tt> element in this container. For example,
           * <code>get(0)</code> returns the smallest element in this container, 
           * while <code>get(2)</code> returns the third smallest element 
           * (<i>i.e.</i> 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-<tt>index</tt> element in this container;
           *         OR null if there is none.
           * @throws IllegalArgumentException if <tt>index</tt> is not in the range 
           *         <code>0..size()-1</code>
           */
          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 <E> 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 <E> E[] toArray(E[] array);
      
      }
      
  • 为什么toArray(Object[]) 只返回它的输入?为什么它不是通用的?

标签: java arrays generics


【解决方案1】:

因此,您不能使用 Object 数组,因为您存储的是 Comparable。相反,您需要使用 Comparable 数组。

smallArray = (T[]) new Object[len + 1];

应该

smallArray = (T[]) new Comparable[len + 1];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多