hi a normal iterator for a LinkedList would be the following, however, how do we build an iterator that returns an iterator starting at a specified index? How do we build:
public Iterator<E>iterator(int index)???
thanks! normal Iterator:
public Iterator<E> iterator( )
{
return new ListIterator();
}
private class ListIterator implements Iterator<E>
{
private Node current;
public ListIterator()
{
current = head; // head in the enclosing list
}
public boolean hasNext()
{
return current != null;
}
public E next()
{
E ret = current.item;
current = current.next;
return ret;
}
public void remove() { /* omitted because optional */ }
}
2 Answers 2
Well you could just call the normal iterator() method, then call next() that many times:
public Iterator<E> iterator(int index) {
Iterator<E> iterator = iterator();
for (int i = 0; i < index && iterator.hasNext(); i++) {
iterator.next();
}
return iterator;
}
4 Comments
for loop and skip the indexes you are not interested in?for(int i=0;i<collection.size();++i) { if(i>index) { break; } else { //dosomething } }. Why create an iterator in the first place? Only reason this would make sense is if you plan to remove elements while iterating.This is kick-off example how to implement such iterator, but it's advised also to create or extend appropriate interface and make this object implementing this interface for convention.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IterableObject {
private List<String> values = new ArrayList<String>();
public Iterator<String> getIterator(final int index) {
Iterator<String> it = new Iterator<String>() {
private int current = index;
@Override
public void remove() {
// TODO Auto-generated method stub
}
@Override
public String next() {
String value = values.get(current);
current++;
return value;
}
@Override
public boolean hasNext() {
if(values.size() > current){
return true;
}else{
return false;
}
}
};
return it;
}
}
UPDATE
According to comments I've written an Iterator for LinkedList
public Iterator<String> getIterator(final int index) {
Iterator<String> it = new Iterator<String>() {
private Object currentObject = null;
{
/*initialize block where we traverse linked list
that it will pointed to object at place index*/
System.out.println("initialize" + currentWord);
for(int i = 0; currentObject.next != null && i < index; i++, currentObject = currentObject.next)
;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
@Override
public String next() {
Object obj = currentObject.next;
currentObject = currentObject.next;
return obj;
}
@Override
public boolean hasNext() {
return currentObject.next != null;
}
};
return it;
}
Because Iterator is object of Anonymous class we can't use constructor but can initialise it in initialise block look at this answer: https://stackoverflow.com/a/362463/947111 We traverse it once at the beginning (sorry for C style) so it will point to currentObject. All remain code is self explained.
10 Comments
values.get(index) needs to traverse the list from the start every time it is executed to get the specified element.hasNext method could be written more succinctly with a body like this: return values.get(current) != null;.
Collecitonwhile iterating, you rather just use a traditionalforloop.listIterator(int)it does exactly what you want. Here is link to docs