gpt4 book ai didi

java - 使用泛型类型的自定义迭代器

转载 作者:行者123 更新时间:2023-11-30 02:08:29 24 4
gpt4 key购买 nike

我正在尝试构建 ArrayList 的扩展并为其构建一个自定义迭代器(全部采用泛型类型),但在编译之前出现了我不明白的错误。

public class GenerateurBiGramme <T> extends ArrayList <T> {
public Iterator<Pair<T,T>> iterator(int delta){
return new BiGramme<>(delta);
}
public class BiGramme <T> implements Iterator<Pair<T, T>>{
int premier = 0;
int dernier = 1;
int delta;
public Pair<T, T> next() {
Pair<T,T> temp = new Pair<T,T>(get(this.debut),get(this.dernier));
return temp;
}

我的 IDE 告诉我“Pair 中的 Pair(T,T) 不能应用于 (T,T)。

我不明白这是什么意思。

感谢您的帮助!

编辑:这是我的“配对”类(class):

public class Pair<T, U> {
public T premiere;
public U deuxieme;

public Pair( T premiere, U deuxieme ) {
this.premiere = premiere;
this.deuxieme = deuxieme;
}
}

基本上,我将向生成器提供一张表,并且我打算使用“premier”和“dernier”以彼此之间设定的距离创建对...

最佳答案

你的问题是在你的声明中public class BiGramme <T>T隐藏T您在 public class GenerateurBiGramme <T> 中声明的。您现在有两个不同类型的参数 GenerateurBiGramme.BiGramme ,但不幸的是,它们都被称为 T .

您不需要 TBiGramme的声明中,因为是内部类,所以它已经可以访问外部类的类型参数,GenerateurBiGramme 。只需将声明更改为 public class BiGramme ,没有 T 。同时删除 <>来自线return new BiGramme(delta); 。这对我有用。

关于java - 使用泛型类型的自定义迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50770259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com