将int更改为Integer迭代器基本java

emm*_*aki 0 java iterator iterable

我正在编写一个实现迭代器的内部类,我正在尝试为我的一个方法返回一个整数,但它不会让我.我知道这是一个非常基本的问题,但我是java的新手,所以我很抱歉听起来很简单.

public class pIterator<Integer> implements Iterator<Integer>{

private int currentEx = 1; 
private int base = 4; 

//error says can't convert int to Integer here specifically 
private Integer changeToInteger = base * currentEx++;  

@Override
public boolean hasNext() {
  return currentEx <= max;  
}

//the problem then occurs here when I try to return an Integer 
@Override
public Integer next() throws NoSuchElementException {
 if (currentEx > max) throw new NoSuchElementException(); 

 return changeToInteger; 
 } 
}
Run Code Online (Sandbox Code Playgroud)

currentEx和base必须是int(由指令定义)所以我应该只更改返回类型还是可以转换为整数?

JB *_*zet 6

java.lang.Integer通过将类定义为具有名为Integer的参数的泛型类来隐藏标准.它应该被定义为

public class pIterator implements Iterator<Integer> {
Run Code Online (Sandbox Code Playgroud)

否则,你的班级相当于

public class pIterator<T> implements Iterator<T> {
Run Code Online (Sandbox Code Playgroud)

T令人困惑地命名为Integer.