問題描述
我有以下代碼(是的,我知道迭代器的實(shí)現(xiàn)不正確.這是我正在寫的一道考試題):
public class MyList擴(kuò)展 ArrayList<Integer>{公共無效noOdds(){MyIterator<整數(shù)>iter = this.iterator();而(iter.hasNext()){如果 (iter.next() % 2 == 1)iter.remove();}}public MyIterator<Integer>迭代器(){return new MyIterator<Integer>(this);}公共類 MyIterator<Integer>實(shí)現(xiàn)迭代器<整數(shù)>{列表<整數(shù)>數(shù)據(jù);整數(shù)大小;整數(shù)位置;MyIterator(列表<整數(shù)>數(shù)據(jù)){this.data = 數(shù)據(jù);this.size = data.size();this.position = 0;}公共布爾 hasNext() {if (this.position < this.size)返回真;別的返回假;}公共整數(shù)下一個(gè)(){整數(shù) tmp = this.data.get(this.position);this.position++;返回 tmp;}公共無效刪除(){如果(this.position == 0)throw new IllegalStateException("下一個(gè)還沒有被調(diào)用");this.data.remove(this.position - 1);}}}
當(dāng)我編譯時(shí),它不會自動將 Integer 裝箱為模運(yùn)算,我得到 ??p>
MyList.java:9:錯(cuò)誤:二元運(yùn)算符%"的操作數(shù)類型錯(cuò)誤if (iter.next() % 2 == 1)
第一種類型:整數(shù)
第二種類型:int
如果我將 iter.next()
更改為 iter.next().intValue()
,我會得到
MyList.java:9:錯(cuò)誤:找不到符號if (iter.next().intValue() % 2 == 1)
符號:方法 intValue()
位置:類對象
但是,如果我改變了
public class MyList...
到
公共類 MyList
然后錯(cuò)誤消失.
想到發(fā)生了什么?
謝謝.
這里,
public class MyList擴(kuò)展 ArrayList<Integer>{//^ 這里
您正在聲明一個(gè)類型變量 Integer
,它會影響 java.lang.Integer
類型.
在類型主體中您引用 Integer
的任何位置,您指的是類型變量而不是 java.lang.Integer
類型.
各種數(shù)值運(yùn)算符不適用于隨機(jī)類型(這是您的類型變量),它們僅適用于原始數(shù)值類型及其包裝類.因此,您不能將它們與類型變量類型的操作數(shù)一起使用.
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.
這篇關(guān)于泛型和迭代器的java編譯器錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!