编译器对我的Java泛型不满意

编译器对我的Java泛型不满意,java,generics,Java,Generics,它总是抱怨: The method add(Matrix<T>) in the type List<Matrix<T>> is not applicable for the arguments (Matrix<String>) 类型列表中的add(Matrix)方法不适用于参数(Matrix) 在提取器类的行中: matrixList.add(new Matrix<String>(attributes)); matrixList.

它总是抱怨:

The method add(Matrix<T>) in the type List<Matrix<T>> is not applicable for the arguments (Matrix<String>)
类型列表中的add(Matrix)方法不适用于参数(Matrix)
在提取器类的行中:

matrixList.add(new Matrix<String>(attributes));
matrixList.add(新矩阵(属性));
在这3个类中定义泛型似乎有问题。有没有简单的方法来解决这个问题?尝试了不同的方法,却无法解决

public class Test {

    public static void main(String[] args) {

        Extractor<String> extractor = new Extractor<>();
        extractor.extract();
    }
}

class Extractor<T> {

    private List<Matrix<T>> matrixList;

    public Extractor() {
        this.matrixList = new ArrayList<>();
    }

    public void extract() {
        List<Attribute<String>> attributes = new ArrayList<>();

        attributes.add(new Attribute<String>("Test 1"));
        attributes.add(new Attribute<String>("Test 2"));

       // !!!! The compiler is complaining here!
        matrixList.add(new Matrix<String>(attributes));
    }

    public List<Matrix<T>> getList() {
        return matrixList;
    }
}

class Matrix<T> {

    private List<Attribute<T>> attributes;

    public Matrix(List<Attribute<T>> attributes) {
        this.attributes = attributes;
    }

    public List<Attribute<T>> getAttributes() {
        return attributes;
    }
}

class Attribute<T> {
    private T attribute;
    public Attribute(T attr) {
        attribute = attr;
    }

    public T getAttr() {
        return attribute;
    }
}
公共类测试{
公共静态void main(字符串[]args){
提取器提取器=新提取器();
extractor.extract();
}
}
类提取器{
私有列表矩阵列表;
公共提取器(){
this.matrixList=新的ArrayList();
}
公开资料摘录(){
列表属性=新的ArrayList();
添加(新属性(“测试1”);
添加(新属性(“测试2”));
//!!编译器在这里抱怨!
添加(新矩阵(属性));
}
公共列表getList(){
返回矩阵列表;
}
}
类矩阵{
私有列表属性;
公共矩阵(列表属性){
this.attributes=属性;
}
公共列表getAttributes(){
返回属性;
}
}
类属性{
私有T属性;
公共属性(T属性){
属性=属性;
}
公共T getAttr(){
返回属性;
}
}

您不能将
矩阵
放入
矩阵
列表中(
t
可以是任何类型)。如果
提取器
仅适用于字符串类型的矩阵,请从中删除类型参数,并将
矩阵列表
类型设置为
列表

您不能将
矩阵
放入
矩阵列表
t
可以是此处的任何类型)。如果
提取器
仅适用于字符串类型的矩阵,请从中删除类型参数,并将
矩阵列表
类型设置为
列表
您的代码根本没有意义。您正在将
提取器
等设置为通用,这意味着您希望它可以用于不同的类型

但是,在
Extractor.extract()
方法中,您专门创建了
字符串的
矩阵
,并将其放入
列表矩阵列表

如果您的代码仅适用于字符串,则不应将其设置为泛型。只需将
列表列为矩阵列表

想一想:如果现在您正在创建一个
提取器intExtractor
,并调用
intExtractor.extract()
,那么您的代码如何合理地工作

或者,要进一步完善您的设计,请:

interface Extractor<T> {
    public List<Matrix<T>> extract();
}

class DummyStringMatrixExtractor implements Extractor<String> {

    // useless now, can be put in extract()
    private List<Matrix<T>> matrixList;

    public Extractor() {
        this.matrixList = new ArrayList<>();
    }

    @Override
    public List<Matrix<String>> extract() {
        List<Attribute<String>> attributes = new ArrayList<>();

        attributes.add(new Attribute<String>("Test 1"));
        attributes.add(new Attribute<String>("Test 2"));

        matrixList.add(new Matrix<String>(attributes));

        return matrixList;
    }

    // useless now
    public List<Matrix<T>> getList() {
        return matrixList;
    }
}
接口提取器{
公共列表摘录();
}
类DummyStringMatrix Extractor实现提取器{
//现在没用了,可以放在摘录()中
私有列表矩阵列表;
公共提取器(){
this.matrixList=新的ArrayList();
}
@凌驾
公共列表摘录(){
列表属性=新的ArrayList();
添加(新属性(“测试1”);
添加(新属性(“测试2”));
添加(新矩阵(属性));
返回矩阵列表;
}
//现在没用了
公共列表getList(){
返回矩阵列表;
}
}

您的代码根本没有意义。您正在将
提取器
等设置为通用,这意味着您希望它可以用于不同的类型

但是,在
Extractor.extract()
方法中,您专门创建了
字符串的
矩阵
,并将其放入
列表矩阵列表

如果您的代码仅适用于字符串,则不应将其设置为泛型。只需将
列表列为矩阵列表

想一想:如果现在您正在创建一个
提取器intExtractor
,并调用
intExtractor.extract()
,那么您的代码如何合理地工作

或者,要进一步完善您的设计,请:

interface Extractor<T> {
    public List<Matrix<T>> extract();
}

class DummyStringMatrixExtractor implements Extractor<String> {

    // useless now, can be put in extract()
    private List<Matrix<T>> matrixList;

    public Extractor() {
        this.matrixList = new ArrayList<>();
    }

    @Override
    public List<Matrix<String>> extract() {
        List<Attribute<String>> attributes = new ArrayList<>();

        attributes.add(new Attribute<String>("Test 1"));
        attributes.add(new Attribute<String>("Test 2"));

        matrixList.add(new Matrix<String>(attributes));

        return matrixList;
    }

    // useless now
    public List<Matrix<T>> getList() {
        return matrixList;
    }
}
接口提取器{
公共列表摘录();
}
类DummyStringMatrix Extractor实现提取器{
//现在没用了,可以放在摘录()中
私有列表矩阵列表;
公共提取器(){
this.matrixList=新的ArrayList();
}
@凌驾
公共列表摘录(){
列表属性=新的ArrayList();
添加(新属性(“测试1”);
添加(新属性(“测试2”));
添加(新矩阵(属性));
返回矩阵列表;
}
//现在没用了
公共列表getList(){
返回矩阵列表;
}
}

一个简单的解决方案是使用
矩阵列表。添加((矩阵)新矩阵(属性))取而代之

该代码对泛型施加了一个非常常见的强制转换问题

代码
matrixList.add(新矩阵(属性))
中介绍的方法意味着您希望向容器matrixList添加一个matrixList元素,该元素被定义为包含泛型模板类型


您可以在文档中找到这些障碍的更简单的说明

一个简单的解决方案是使用
matrixList.add((矩阵)新矩阵(属性))取而代之

该代码对泛型施加了一个非常常见的强制转换问题

代码
matrixList.add(新矩阵(属性))
中介绍的方法意味着您希望向容器matrixList添加一个matrixList元素,该元素被定义为包含泛型模板类型


您可以在文档中找到关于这些障碍的更简单说明

我不这么认为。T可能是字符串或其他类型。在这门课上,他可以用矩阵来表示这里的矩阵,也可以用矩阵来表示某处的任何类型。@kimdung这是完全错误的。你真的明白泛型想要解决什么问题吗?@kimdung在这种情况下,extract方法应该返回
Matrix
,而不是将其添加到类型为
T
的私有字段中。同意。我没有仔细检查他的代码