Java ListIterator如何对nextIndex()和previousIndex()进行编码
作为一门课程,我必须为一个List类编写代码,这涉及编写ListIterator的一部分并使用ListIteratorAPI(不是来自java.util) 我写nextIndex和previousIndex有困难 下面是ListIterator类Java ListIterator如何对nextIndex()和previousIndex()进行编码,java,Java,作为一门课程,我必须为一个List类编写代码,这涉及编写ListIterator的一部分并使用ListIteratorAPI(不是来自java.util) 我写nextIndex和previousIndex有困难 下面是ListIterator类 private class ListIterator implements ListIteratorAPI<E> { /** * current cursor position */ private N
private class ListIterator implements ListIteratorAPI<E> {
/**
* current cursor position
*/
private Node position;
/**
* previous cursor position
*/
private Node previous;
/**
* Constructs an iterator that points to the front of the linked list.
*/
public ListIterator() {
position = null;
previous = null;
}
@Override
public E next() {
if (!hasNext()) {
throw new ListException("Called at the end of the list");
}
previous = position;
if (position == null) {
position = first;
} else {
position = position.next;
}
return position.data;
}
@Override
public E previous() {
if (!hasPrevious()) {
throw new ListException("Called at the beginning of the list");
}
previous = position;
if (position == null) {
position = last;
} else {
position = position.previous;
}
return position.data;
}
@Override
public boolean hasNext() {
if (position == null) {
return first != null;
} else {
return position.next != null;
}
}
@Override
public boolean hasPrevious() {
if (position == null) {
return last != null;
} else {
return position.previous != null;
}
}
@Override
public void add(E element) {
if (position == null) {
addFirst(element);
position = first;
} else {
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
if (newNode.next != null) {
newNode.next.previous = newNode;
}
position.next = newNode;
newNode.previous = position;
position = newNode;
if (newNode.previous == last) {
last = newNode;
}
size++;
}
previous = position;
}
@Override
public void remove() {
if (previous == position) {
throw new IllegalStateException();
}
if (position == first) {
removeFirst();
} else {
Node tmp = position;
previous.next = position.next;
if (position.next != null) {
position.next.previous = previous;
}
tmp.next = tmp.previous = null;
size--;
}
position = previous;
}
@Override
public void set(E element) {
if (position == null) {
throw new ListException("set calls on a null node");
}
position.data = element;
}
@Override
public int nextIndex() {
}
@Override
public int previousIndex() {
}
}
私有类ListIterator实现ListIteratorAPI{
/**
*当前光标位置
*/
私有节点位置;
/**
*上一个光标位置
*/
前一个私有节点;
/**
*构造一个指向链表前面的迭代器。
*/
公共列表迭代器(){
位置=空;
previous=null;
}
@凌驾
公共教育{
如果(!hasNext()){
抛出新的ListException(“在列表末尾调用”);
}
先前=位置;
如果(位置==null){
位置=第一;
}否则{
位置=位置。下一步;
}
返回位置数据;
}
@凌驾
公共服务{
如果(!hasPrevious()){
抛出新ListException(“在列表开头调用”);
}
先前=位置;
如果(位置==null){
位置=最后一个;
}否则{
位置=位置。上一个;
}
返回位置数据;
}
@凌驾
公共布尔hasNext(){
如果(位置==null){
首先返回!=null;
}否则{
返回位置.next!=null;
}
}
@凌驾
公共布尔值hasPrevious(){
如果(位置==null){
返回最后一个!=null;
}否则{
返回位置.previous!=null;
}
}
@凌驾
公共无效添加(E元素){
如果(位置==null){
addFirst(元素);
位置=第一;
}否则{
Node newNode=新节点();
newNode.data=元素;
newNode.next=position.next;
if(newNode.next!=null){
newNode.next.previous=newNode;
}
position.next=newNode;
newNode.previous=位置;
位置=新节点;
if(newNode.previous==last){
last=newNode;
}
大小++;
}
先前=位置;
}
@凌驾
公共空间删除(){
如果(上一个==位置){
抛出新的非法状态异常();
}
如果(位置==第一个){
移除第一个();
}否则{
节点tmp=位置;
previous.next=position.next;
if(position.next!=null){
position.next.previous=previous;
}
tmp.next=tmp.previous=null;
大小--;
}
位置=以前的位置;
}
@凌驾
公共空集(E元素){
如果(位置==null){
抛出新ListException(“在空节点上设置调用”);
}
position.data=元素;
}
@凌驾
公共int nextIndex(){
}
@凌驾
public int previousIndex(){
}
}
这是我正在使用的ListIteratorAPI
public interface ListIteratorAPI <E>
{
/**
* returns true if next() method returns an element rather than an exception
* @return true if list has more elements when moving the list in the forward
* direction
*/
public boolean hasNext();
/**
* returns true if previous() method returns an element rather than an exception
* @return true if list has more elements when moving the list in the reverse
* direction
*/
public boolean hasPrevious();
/**
* returns the next element in the list and advances the cursor position
* @returnthe next element
* @throws ListException if the iteration has no next element
*/
public E next() throws ListException;
/**
* returns the index of the element that would be returned by a subsequent
* call to next() method
* @return the index of the element or list size if the iterator is at the
* end of the list
*/
public int nextIndex();
/**
* returns the previous element in the list and moves the cursor position
* backwards
* @return the previous element
* @throws ListException if the iteration has no previous element
*/
public E previous() throws ListException;
/**
* returns the index of the element that would be returned by a subsequent
* call to previous() method
*
* @return the index of the element or -1 if the iterator is at the
* beginning of the list.
*/
public int previousIndex();
/**
* Adds an element before the iterator position and moves the iterator past
* the inserted element.
*
* @param element the element to add
*/
void add(E element);
/**
* Removes the last traversed element. This method may only be called after
* a call to the next() method.
*/
void remove();
/**
* Replaces the last element returned by next() or previous() with the
* specified element.
*
* @param element the element to set
*/
void set(E element);
}
公共接口ListIteratorAPI
{
/**
*如果next()方法返回元素而不是异常,则返回true
*@return true如果向前移动列表时列表包含更多元素
*方向
*/
公共布尔hasNext();
/**
*如果previous()方法返回元素而不是异常,则返回true
*@return true如果列表反向移动时包含更多元素
*方向
*/
公共布尔hasPrevious();
/**
*返回列表中的下一个元素并前进光标位置
*@return下一个元素
*如果迭代没有下一个元素,@将抛出ListException
*/
public E next()抛出ListException;
/**
*返回将由后续操作返回的元素的索引
*调用next()方法
*@如果迭代器位于
*名单的末尾
*/
public int nextIndex();
/**
*返回列表中的上一个元素并移动光标位置
*向后
*@返回上一个元素
*如果迭代没有前一个元素,@将抛出ListException
*/
public E previous()抛出ListException;
/**
*返回将由后续操作返回的元素的索引
*调用previous()方法
*
*@返回元素的索引,如果迭代器位于
*列表的开头。
*/
public int previousIndex();
/**
*在迭代器位置之前添加一个元素,并将迭代器移动过去
*插入的元素。
*
*@param element要添加的元素
*/
无效添加(E元素);
/**
*删除最后遍历的元素。此方法只能在
*对next()方法的调用。
*/
无效删除();
/**
*将next()或previous()返回的最后一个元素替换为
*指定的元素。
*
*@param element要设置的元素
*/
空集(E元素);
}
您可以保留一个int
来存储当前索引。从“0”开始,在运行过程中递增/递减。您能给我一个使用上述代码放置它的好位置示例吗?添加private int index代码>在其他两个字段之后。