⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrayedlistiteratoruos.java

📁 国外的数据结构与算法分析用书
💻 JAVA
字号:
/* ArrayedListIteratorUos.java
 * ---------------------------------------------
 * Copyright (c) 2001 University of Saskatchewan
 * All Rights Reserved
 * --------------------------------------------- */
 
package dslib.list;

import dslib.exception.*;
import dslib.base.*;

/**	A Linear Iterator for moving in an array that is the representation for an
	ArrayedList.  It has functions to move forward and back, and to the 
	first and last items of the array.  There are also routines to check if the 
	current position is before the start or after the end of the array. 
	In order to be consistent with ArrayedList, the first item is
	in position count-1, and the last item is in position 0. */
public class ArrayedListIteratorUos implements BilinearIteratorUos 
{
	/**	The array being iterated.   First item is stored in 
		position count-1, last item in position 0. */
	private Object theArray[];

	/**	The number of items in the data structure. */
	private int count;

	/**	Initialize the iterator where the array contains count items. <br> 
		Analysis: Time = O(1) 
		@param newArray[] array being iterated
		@param newCount number of items in the array */
	public ArrayedListIteratorUos(Object newArray[], int newCount)
	{
		theArray = newArray;
		count = newCount;
		goFirst();
	}
	 
	/**	The current position in the array. */
	protected int pos;
	
	/**	Set the position. <br> 
		Analysis: Time = O(1) 
		@param newPos position to be set as the current position */ 
	public void setPos(int newPos)
	{
		pos = newPos;
	}

	/**	Return the position. <br> 
		Analysis: Time = O(1) */ 
	public int pos()
	{
		return pos;
	}
	
	/**	The current item in the array. <br> 
		Analysis: Time = O(1) <br>
		PRECONDITION: <br>
		<ul>
			itemExists() 
		</ul> */
	public Object item() throws NoIteratorItemUosException
	{
		if (!(itemExists()))
			throw new NoIteratorItemUosException("A current iterator item must exist.");
		
		return theArray[pos];
	}	 
	 
	/**	Is the current position before the array?. <br> 
		Analysis: Time = O(1) */
	public boolean before()
	{
		return pos>=count;
	}

	/**	Is the current position after the array?. <br> 
		Analysis: Time = O(1) */
	public boolean after()
	{
		return pos<0;
	} 
	
	/**	Move to the first item in the array. <br> 
		Analysis: Time = O(1) */
	public void goFirst()
	{
		/* Recall that items are stored in reverse order in array */
		pos = count - 1;
	} 

	/**	Move to the last item in the array. <br> 
		Analysis: Time = O(1) */
	public void goLast()
	{ 
		/* Recall that items are stored in reverse order in array */
		pos = 0;
	}
	
	/**	Is there a current item?. <br> 
		Analysis: Time = O(1) */
	public boolean itemExists()
	{ 
		return !before() && !after();
	}

	 
	/**	Move forward in the array. <br> 
		Analysis: Time = O(1) <br>
		PRECONDITION:  <br>
		<ul>
			!after() 
		</ul>  */
	public void  goForth() throws AfterTheEndUosException
	{
		/* Recall that items are stored in reverse order in array */
		if (after())
			throw new AfterTheEndUosException("Cannot advance to next item since already after.");
		
		pos--; 
	}
	
	/**	Move back in the array. <br> 
		Analysis: Time = O(1) <br>
		PRECONDITION:  <br>
		<ul>
			!before() 
		</ul> */
	public void goBack() throws BeforeTheStartUosException
	{
		/* Recall that items are stored in reverse order in array */
		if (before())
			throw new BeforeTheStartUosException("Cannot move back since already before.");
		
		pos++;
	}

	/**	Move to before the start of the array. <br> 
		Analysis: Time = O(1) */
	public void goBefore()
	{ 
		/* Recall that items are stored in reverse order in array */
		pos = count;
	}

	/**	Move to after the end of the array. <br> 
		Analysis: Time = O(1) */
	public void  goAfter()
	{ 
		/* Recall that items are stored in reverse order in array */
		pos = -1;
	}

	/**	Output the current item. <br> 
		Analysis: Time = O(n), where n = length of the array */
	public String toString()
	{
		return theArray.toString();
	}

 } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -