我正在尝试制作以下静态方法,它给我一个错误

I am trying to make the following static method it gives me an error

我正在尝试使用以下代码来制作一个调整数组大小的通用方法,但它给我一个错误 "non-static type variable T cannot be referenced from static context",有没有什么方法可以使这个方法静态化仍然没有错误?还有其他更好的方法吗?我是 java 的新手,我还在学习,所以我不确定它是否有效,或者我只是认为它会为我完成工作?

    public static T[] resizeArray(T[] t,int newSize) { 
        Object[] temp = new Object[newSize];
        System.arraycopy(t, 0, temp, 0, t.length);
        t=(T[]) new Object[newSize];
        System.arraycopy(temp, 0, t, 0, t.length);
        return t;
    }

将类型定义放在 return 类型之前

public static <T> T[] resizeArray(T[] t,int newSize)

您可以在 static 之后使用 <T> 使方法通用,另请参阅 Arrays.copyOf(T[], int) 的签名。其中 复制指定的数组,用空值截断或填充(如有必要),以便副本具有指定的长度 。像,

public static <T> T[] resizeArray(T[] t, int newSize) {
    return Arrays.copyOf(t, newSize);
}