我有一个linkedlistdouble类 public ListIterator<T>listIterator()
方法,我正在尝试执行接口listiterator,因为它是匿名内部类,我走的是正确的路径吗?我应该怎么做才能使 public int nextIndex()
/ public int previousIndex()
工作?nextindex方法返回后续调用next将返回的元素的索引,如果列表迭代器位于列表的末尾,并且previousindex方法返回后续调用previous将返回的元素的索引,则返回list size,或者-1,如果列表迭代器如这里所说位于列表的开头
这是linkedlistdouble类
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;
}
下面是应该执行接口listiterator的方法,因为它是匿名的内部类,我到目前为止尝试了:
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;
}
1条答案
按热度按时间kg7wmglp1#
这是linkedlist的listiterator()方法的代码示例:
如您所见,您需要保留一个变量
index
跟踪下一个索引。下一个索引在next()和previous()方法中递增/递减,这些方法已经处理了列表大小问题,因此无需担心nextindex()和previousindex()方法中的大小问题。