泛型和迭代器的java编译器错误

2022-01-10 00:00:00 iterator java

我有以下代码(是的,我知道迭代器的实现不正确.这是我正在写的一道考试题):

public class MyList扩展 ArrayList<Integer>{公共无效noOdds(){MyIterator<整数>iter = this.iterator();而(iter.hasNext()){如果 (iter.next() % 2 == 1)iter.remove();}}public MyIterator<Integer>迭代器(){return new MyIterator<Integer>(this);}公共类 MyIterator<Integer>实现迭代器<整数>{列表<整数>数据;整数大小;整数位置;MyIterator(列表<整数>数据){this.data = 数据;this.size = data.size();this.position = 0;}公共布尔 hasNext() {if (this.position < this.size)返回真;别的返回假;}公共整数下一个(){整数 tmp = this.data.get(this.position);this.position++;返回 tmp;}公共无效删除(){如果(this.position == 0)throw new IllegalStateException("下一个还没有被调用");this.data.remove(this.position - 1);}}}

当我编译时,它不会自动将 Integer 装箱为模运算,我得到

MyList.java:9:错误:二元运算符%"的操作数类型错误if (iter.next() % 2 == 1)

第一种类型:整数

第二种类型:int

如果我将 iter.next() 更改为 iter.next().intValue(),我会得到

MyList.java:9:错误:找不到符号if (iter.next().intValue() % 2 == 1)

符号:方法 intValue()

位置:类对象

但是,如果我改变了

 public class MyList...

 公共类 MyList

然后错误消失.

想到发生了什么?

谢谢.

解决方案

这里,

public class MyList扩展 ArrayList<Integer>{//^ 这里

您正在声明一个类型变量 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.

相关文章