java 将Comparable[]类型的数组向下转换为泛型&lt;T extends Comparable< T>&gt;

0aydgbwb  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(116)

所以,最近我一直在尝试实现我自己的泛型可比类型数组,我的类包含一个私有字段T[]数组,问题是,对于我的排序算法的实现,我需要使用一个可以返回数组的函数,但当我尝试这样做时,我得到了一个ClassCastException。所以在调试后,我发现当我使用构造函数或resize()创建T[]数组时,方法我得到一个Comparable[]的示例作为结果,但它存储了我的泛型类型T。我读过关于向下转换的文章,人们说它并不总是正常工作,但我不能得到它是如何工作的。有没有办法在不获得ClassCastException的情况下将数组返回给程序供进一步使用

public class myArray<T extends Comparable<T>> {
    private T[] array;
    private int size;
    private int maxSize;

    public myArray() {
        maxSize = 100;
        size = 0;
        array = (T[]) new Comparable[maxSize];
    }

    public myArray(int newSize) {
        maxSize = newSize * 2 + 10;
        array = (T[]) new Comparable[maxSize];
        size = 0;
    }

    public myArray(myArray<T> myArr) {
        maxSize = myArr.maxSize;
        size = myArr.size;
        array = (T[]) new Comparable[maxSize];
        if (size >= 0) System.arraycopy(myArr.array, 0, array, 0, size);
    }
    private void resize() {
        maxSize = size * 2 + 10;
        T[] tmp_array = array;
        array = (T[]) new Comparable[maxSize];
        System.arraycopy(tmp_array, 0, array, 0, size);
    }
    public T[] getArray() {
        return array;
    }
    public String toString() {
        if (size == 0) {
            return "Empty array";
        }
        String str = "";
        for (int i = 0; i < this.size; i++) {
            str += this.array[i].toString() + " ";
        }
        return str;
    }
}

字符串
这就是我尝试使用它并获取ClassCastException的地方

@Test
    @DisplayName("Resize test")
    public void test3() {
        myArray<Integer> array = new myArray<Integer>(2);
        System.out.println("Array size is " + array.size() + " and max size is " + array.maxSize());
        for (int i = 0; i < array.maxSize(); i++) {
            array.pushBack(i * i - 3 * i);
        }
        System.out.println("Array is " + array + "and max size is " + array.maxSize());
        Integer[] arr = array.getArray();
        for (Integer i : arr) {
            System.out.println(i + " ");
        }
    }

cgyqldqp

cgyqldqp1#

在 *T数组 * 上使用 *Object数组 *。

class myArray<T extends Comparable<T>> {
    private Object[] array;
    private int size;
    private int maxSize;

    public myArray() {
        maxSize = 100;
        size = 0;
        array = new Object[maxSize];
    }

    public myArray(int newSize) {
        maxSize = newSize * 2 + 10;
        array = new Object[maxSize];
        size = 0;
    }

    public myArray(myArray<T> myArr) {
        maxSize = myArr.maxSize;
        size = myArr.size;
        array = new Object[maxSize];
        if (size >= 0) System.arraycopy(myArr.array, 0, array, 0, size);
    }
    private void resize() {
        maxSize = size * 2 + 10;
        Object[] tmp_array = array;
        array = new Object[maxSize];
        System.arraycopy(tmp_array, 0, array, 0, size);
    }
    @SuppressWarnings("unchecked")
    public T[] getArray() {
        return (T[]) array;
    }
    public String toString() {
        if (size == 0) {
            return "Empty array";
        }
        String str = "";
        for (int i = 0; i < this.size; i++) {
            str += this.array[i].toString() + " ";
        }
        return str;
    }
}

字符串
下面是一个示例用法。

myArray<String> a = new myArray<>();
String[] b = a.getArray();

相关问题