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

📄 arrayediteratoruos.java

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

package dslib.dictionary.arrayed;

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

/**	A Linear Iterator for moving in an array.  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. */
public class ArrayedIteratorUos implements BilinearIteratorUos 
{
	/**	The array being iterated. */
	protected Object[] theArray;

	/**	The number of items in the data structure. */
	protected 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 ArrayedIteratorUos(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 current 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<0;
	}

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

	/**	Move to the last item in the array. <br> 
		Analysis: Time = O(1) */
	public void goLast()
	{ 
		pos = count - 1;
	}
	
	/**	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
	{
		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
	{
		if (before())
			throw new BeforeTheStartUosException("Cannot move back since already before.");
		
		pos--; 
	}

	/**	Move before the start of the array. <br> 
		Analysis: Time = O(1) */
	public void goBefore()
	{ 
		pos = -1;
	}

	/**	Move after the end of the array. <br> 
		Analysis: Time = O(1) */
	public void  goAfter()
	{ 
		pos = count;
	}

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

	/**	A shallow clone of this object. <br> 
		Analysis: Time = O(1) */
	public Object clone()
	{
		try
		{
			return super.clone();
		} catch (CloneNotSupportedException e)
		{
			// Should not occur: this is a CursorUos, which implements Cloneable 
			e.printStackTrace();
 			return null;
		}
	}
} 

⌨️ 快捷键说明

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