I have this LinkedListDouble class which has the public ListIterator<T>listIterator()
method,and I'm trying to execute the interface ListIterator as it's anonymous inner class,am I going the right path?what should I do to make the public int nextIndex()
/public int previousIndex()
work?
nextIndex method returns the index of the element that would be returned by a subsequent call to next, or list size if the list iterator is at the end of the list and the previousIndex method returns the index of the element that would be returned by a subsequent call to previous, or -1 if the list iterator is at the beginning of the list as it said here
here is the LinkedListDouble class
public class LinkedListDouble <T> {
private Node first = null;
private Node last = null;
public LinkedListDouble () // constructor
{
first = null; // no items on list yet
last = null;
}
public void add(T item) {
Node newNode = new Node(item);
if (isEmpty()) {
first =newNode;
last = newNode;
}
else {
//first.setPrev(newNode);
//newNode.setNext(first);
//first = newNode;
last.setNext(newNode);
newNode.setPrev(last);
last=newNode;
}
}
public boolean contains(T item){
if(first==null)
return false;
else
{
Node newNode=first;
while(newNode!=null)
{
if(newNode.getInfo().equals(item))
return true;
else
newNode=newNode.getNext();
}
}
return false;
}
public T remove(T item)
{//get care of first and last nodes
//and if there is more than 1 matching
boolean check=contains(item);
if(check==true)
{
Node newNode=first;
while(newNode!=null)
{
if(newNode.getInfo().equals(item))
{
newNode.getPrev().setNext(newNode.getNext());
newNode.getNext().setPrev(newNode.getPrev());
return item;
}
else
newNode=newNode.getNext();
}
}
return null;
}
public int size()
{
int size=0;
if(first==null)
return size;
else
{
Node newNode=first;
while(newNode!=null)
{
size++;
newNode=newNode.getNext();
}
}
return size;
}
public String toString()
{
Node newNode=first;
String s="";
while(newNode!=null)
{
s+=newNode.getInfo()+" ,";
newNode=newNode.getNext();
}
return s;
}
public boolean isEmpty() {
return first == null;
}
and here is the method that should execute the interface ListIterator as it's anonymous inner class and what I tried to do so far:
public ListIterator<T>listIterator()
{
ListIterator<T>listIterator = new ListIterator<T>() {
private Node current = first;
private Node temp2 = null;
private int curindex = 0;
@Override
public void add(T e) {
// TODO Auto-generated method stub
throw new RuntimeException();
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
boolean flag=true;
if(current.getNext()==null)
{
flag=false;
}
return flag;
}
@Override
public boolean hasPrevious() {
// TODO Auto-generated method stub
boolean flag=true;
if(current.getPrev()==null)
{
flag=false;
}
return flag;
}
@Override
public T next() {
// TODO Auto-generated method stub
if (!hasNext()) throw new NoSuchElementException();
temp2=current.getNext();
current=current.getNext();
return (T) temp2.getInfo();
}
@Override
public int nextIndex() {
// TODO Auto-generated method stub
int counter=0;
if(!hasNext()) return size();
return curindex;
}
@Override
public T previous() {
// TODO Auto-generated method stub
if (!hasPrevious()) throw new NoSuchElementException();
temp2 = current.getPrev();
temp2 = temp2.getPrev();
return (T) temp2.getInfo();
}
@Override
public int previousIndex() {
// TODO Auto-generated method stub
int counter=0;
if(!hasPrevious()) return -1;
return curindex-1;
}
@Override
public void remove() {
// TODO Auto-generated method stub
throw new RuntimeException();
}
@Override
public void set(T e) {
// TODO Auto-generated method stub
throw new RuntimeException();
}
};
return listIterator;
}
This is a code sample from listIterator() method of LinkedList:
As you can see, you need to keep an variable like
index
to keep the track of next index.The next index is incremented/decremented inside next() and previous() method and these methods already handles List size issue, hence no need to worry about size issue in nextIndex() and previousIndex() method.