i just intent to initialize an iterator over a generic linked list like that (generic typ T seems to be erased in here since the site interpret it as tag)
public <T> LinkedList<T> sort(LinkedList<T> list){
Iterator<T> iter = new list.iterator();
...
but i got the error:
"list cannot be resolved"
what's wrong?
Remove the new
keyword:
Iterator<T> iter = list.iterator();
To further clarify Mykola's correct answer: you're trying to create a new object of the class list
. So you just want to call list.iterator()
(which, somewhere inside it, is itself doing new Iterator
or something like it and returning that to you).
Since you're clearly using Java 5 or above, though, the better way might be instead of doing
public <T> LinkedList<T> sort(LinkedList<T> list){
Iterator<T> iter = new list.iterator();
while (iter.hasNext()){
T t = iter.next();
...
}
}
instead doing this:
public <T> LinkedList<T> sort(LinkedList<T> list){
for (T t : list){
...
}
}
Still better, don't write that method at all and instead use
Collections.sort(list);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With