小编emm*_*aki的帖子

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

我正在编写一个实现迭代器的内部类,我正在尝试为我的一个方法返回一个整数,但它不会让我.我知道这是一个非常基本的问题,但我是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(由指令定义)所以我应该只更改返回类型还是可以转换为整数?

java iterator iterable

0
推荐指数
1
解决办法
247
查看次数

标签 统计

iterable ×1

iterator ×1

java ×1