📄 goring.java
字号:
import java.util.*;
class Ring extends java.util.AbstractSequentialList
{
private Node header;
private int size =0;
public Ring()
{}
public Ring(List list)
{
super();
addAll(list);
}
public ListIterator listIterator(int index)
{
return new RingIterator(index);
}
public int size()
{
return size;
}
private static class Node
{
Object object;
Node previous , next;
Node(Object object,Node previous,Node next)
{
this.object=object;
this.previous = previous;
this.next = next;
}
Node(Object object)
{
this.object = object;
this.previous = this.next = this;
}
}
private class RingIterator implements ListIterator
{
private Node next,lastReturned;
private int nextIndex;
RingIterator(int index)
{
if(index<0 || index > size) System.out.print("ddd");
next=(size==0?null:header);
for(nextIndex =0;nextIndex<index;nextIndex++)
next= next.next;
}
public boolean hasNext()
{
return size>0;
}
public boolean hasPrevious()
{
return size >0;
}
public Object next()
{
if (size==0) throw new NoSuchElementException();
lastReturned = next;
next=next.next;
nextIndex=(nextIndex==0?size-1:nextIndex-1);
return lastReturned.object;
}
public Object previous()
{
if(size ==0) throw new NoSuchElementException();
next = lastReturned = next.previous;
nextIndex = (nextIndex==0?size -1:nextIndex-1);
return lastReturned.object;
}
public int nextIndex()
{
return nextIndex;
}
public int previousIndex()
{
return (nextIndex==0?size -1:nextIndex -1);
}
public void add(Object object)
{
if(size==0)
{
next =header=new Node(object);
nextIndex =0;
}
else
{
Node newNode = new Node(object,next.previous,next);
newNode.previous.next= next.previous= newNode;
}
lastReturned =null;
++size;
nextIndex=(nextIndex==size-1?0:nextIndex+1);
}
public void remove()
{
if (lastReturned ==null ) throw new IllegalStateException();
if (next==lastReturned) next=lastReturned.next;
else nextIndex = (nextIndex ==0?size -1:nextIndex-1);
lastReturned.previous.next= lastReturned.next;
lastReturned.next.previous =lastReturned.previous;
lastReturned = null;
-- size;
}
public void set(Object object)
{
if ( lastReturned ==null ) throw new IllegalStateException();
lastReturned.object = object;
}
}
}
public class goRing
{
private static final int SIZE =8;
private static Ring ring = new Ring();
public static void main(String agrs[])
{
ListIterator it = ring.listIterator();
for(int n=0;n<SIZE;n++)
it.add(new Integer (n) );
printNext(it,5);
}
private static void printNext(ListIterator it,int m )
{
int n=5;
Object keeppoint=new Object();
for(int k=0;n<SIZE;k ++)
{
for(int i=0;i<n;i++ )
keeppoint =it.next();
System.out.println("it:="+keeppoint);
it.remove();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -