Generic (T extends Comparable < T >) how do you define an array?

public class Array < T > {

private T[] arr;
    public Array(){
        arr = (T[]) new Object[10];
    }

}
can be executed in this way, but an error occurs when T extends Comparable < T >
how should it be defined

Sep.17,2021

= update
I don't know if you want to emulate an ArrayList,. I wrote a very crude one, in which the Object [] array is used to store it, and then converted to T when taken out.

public class Array<T extends Comparable<T>> {
    
    private Object[] arr;
    public static void main(String[] args) {
        Array<A> arr = new Array<A>();
        arr.set(0, new A(24));
        arr.set(1, new A(25));
        System.out.println(arr.get(1));
    }
    public Array(){
        arr = new Object[10];
    }
    public void set(int index, T ele) {
        arr[index] = ele;
    }
    public T get(int index) {
        return (T) arr[index];
    }
}

class A implements Comparable<A>{
    private int age;
    public A(int age) {this.age = age;}
    public A() {}
    public int getAge() {
        return this.age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int compareTo(A a) {
        return this.age - a.getAge();
    }
    
}

in the source code of ArrayList , it also uses the Object array to store elements:
transient Object [] elementData;

= original answer
Comparable is an interface and should be used implements ;
in addition, is your Array a class or? What mistake did you report?

Menu