user3025417 user3025417 - 1 year ago 147
Java Question

Integer.valueOf() cannot find symbol in Iterator

Hi I want to write my own

Iterator
over a linked list (which returns the values in the list). For some reason I get a "cannot find symbol" error when calling
Integer.valueOf()


import java.util.*;
class List {
private Node head;
private class IntIterator<Integer> implements Iterator<Integer>{
private Node currentNode;

private IntIterator(){
currentNode = head;
}

@Override
public boolean hasNext(){
return currentNode != null;
}

@Override
public Integer next(){
if (!hasNext()) {
return Integer.valueOf(-1); //the compiler doesnt like this..
}
int val = currentNode.val;
currentNode = currentNode.next;
return Integer.valueOf(val); //and that!
}

public IntIterator<Integer> iterator(){
return new IntIterator<Integer>();
}
}


I don't know how this issue can happen since the compiler doesn't complain about
Integer
in the other parts of the program.

Anyway I hope you can help me with that issue!

Answer Source

The problem here is that type parameter Integer hides visible type java.lang.Integer. You should write like

private class IntIterator implements Iterator<Integer>

instead of

private class IntIterator<Integer> implements Iterator<Integer>
                         ^^^^^^^^^

It seems you don't understand generics completely. I suggest you to read about them.