带有泛型和迭代器的java编译器错误 [英] java compiler error with generics and an iterator
问题描述
我有以下代码(是的,我知道迭代器实现不正确。这是我正在写的一个考试题):
公共类MyList< Integer> extends ArrayList< Integer> {
public void noOdds(){
MyIterator< Integer> iter = this.iterator();
while(iter.hasNext()){
if(iter.next()%2 == 1)
iter.remove();
}
}
public MyIterator< Integer> iterator(){
返回新的MyIterator< Integer>(this);
}
公共类MyIterator< Integer>实现Iterator< Integer> {
List< Integer>数据;
int size;
int position;
MyIterator(List< Integer> data){
this.data = data;
this.size = data.size();
this.position = 0;
}
public boolean hasNext(){
if(this.position< this.size)
return true;
else
返回false;
}
public Integer next(){
Integer tmp = this.data.get(this.position);
this.position ++;
返回tmp;
}
public void remove(){
if(this.position == 0)
抛出新的IllegalStateException(next尚未调用) ;
this.data.remove(this.position - 1);
}
}
}
当我编译时,它赢了我自动将整数运算为模运算,我得到
MyList.java:9:错误:二元运算符'%'的错误操作数类型'%'
如果(iter.next()%2 == 1)
第一种类型:整数
第二种类型:int
如果我将 iter.next()
更改为 iter.next()。intValue ()
,我得
MyList.java:9:错误:找不到符号
if(iter.next()。 intValue()%2 == 1)
symbol:方法intValue()
location:class Object
但是,如果我改变
公共类MyList< Integer>。 ..
到
公共类MyList
然后错误消失。
关于发生了什么事的想法?
谢谢。
此处,
公共类MyList<整数> extends ArrayList< Integer> {
// ^这里
你要声明一个类型变量整数
,它隐藏 java.lang.Integer
类型。
类型中的任何位置您引用 Integer
的正文,指的是类型变量而不是 java.lang.Integer
类型。
各种数值运算符不适用于随机类型(这是你的类型变量),它们只适用于原始数字类型及其包装类。因此,您不能将它们与类型变量类型的操作数一起使用。
I have the following code (Yes, I know the iterator is implemented incorrectly. This is an exam question I'm writing):
public class MyList<Integer> extends ArrayList<Integer> {
public void noOdds() {
MyIterator<Integer> iter = this.iterator();
while (iter.hasNext()) {
if (iter.next() % 2 == 1)
iter.remove();
}
}
public MyIterator<Integer> iterator() {
return new MyIterator<Integer>(this);
}
public class MyIterator<Integer> implements Iterator<Integer> {
List<Integer> data;
int size;
int position;
MyIterator(List<Integer> data) {
this.data = data;
this.size = data.size();
this.position = 0;
}
public boolean hasNext() {
if (this.position < this.size)
return true;
else
return false;
}
public Integer next() {
Integer tmp = this.data.get(this.position);
this.position++;
return tmp;
}
public void remove() {
if (this.position == 0)
throw new IllegalStateException("next hasn't been called yet");
this.data.remove(this.position - 1);
}
}
}
When I compile, it won't auto box the Integer for modulo op, and I get
MyList.java:9: error: bad operand types for binary operator '%' if (iter.next() % 2 == 1)
first type: Integer
second type: int
If I change iter.next()
to iter.next().intValue()
, I get
MyList.java:9: error: cannot find symbol if (iter.next().intValue() % 2 == 1)
symbol: method intValue()
location: class Object
However, if I change
public class MyList<Integer>...
to
public class MyList
then the errors go away.
Thoughts on what's going on?
Thanks.
Here,
public class MyList<Integer> extends ArrayList<Integer> {
// ^ here
you are declaring a type variable Integer
which shadows the java.lang.Integer
type.
Anywhere within the type body where you refer to Integer
, you are referring to the type variable rather than the java.lang.Integer
type.
The various numerical operators do not apply to random types (which is what your type variable is), they only work with primitive numeric types and their wrapper classes. Therefore you can't use them with operands of the type of your type variable.
这篇关于带有泛型和迭代器的java编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!