当我不知道泛型对象的类型时,如何避免 Java 中出现未经检查的方法警告?

How can I avoid unchecked method warnings in Java when I don't know the type of a generic object?

我刚开始使用 Java 泛型,想知道我应该如何摆脱未经检查的方法警告。我知道像这样的代码指定了通用对象的类型并且没有给我警告:

DTRowData<String> someData = new DTRowData<String>("Some string");

但是我不知道我的泛型对象的类型,所以我一直在编写这样的代码:

DTRowData moreData = new DTRowData(80100);

这段代码对我来说更有意义,因为如果您不知道要返回的数据类型,那么使用泛型似乎是一个很好的理由。但它给了我警告:"Unchecked call to DTRowData as a member of raw type DTRowData"

当我不知道我将返回什么类型的数据时,停止收到此警告的正确方法是什么?有时它是一个数字,有时是一个字符串。 我不想使用

@SuppressWarnings("unchecked")

这是我的 class 代码,如果它有帮助的话:

public class DTRowData<E> {
    public E someValue;

    public DTRowDate(E someValue){
        this.someValue = someValue;
    }
}

在语句DTRowData moreData = new DTRowData(80100);中,你已经知道参数E的类型:它是一个从构造函数参数80100.[=14=的类型推断出来的整数]

所以可以像下面这样使用泛型:

 DTRowData<Integer> someData = new DTRowData<Integer>(80100);