ContactCenters
V. 0.9.9.

umontreal.iro.lecuyer.contactcenters.ctmc
Class CircularIntArray

java.lang.Object
  extended by umontreal.iro.lecuyer.contactcenters.ctmc.CircularIntArray
All Implemented Interfaces:
Cloneable

public class CircularIntArray
extends Object
implements Cloneable

Represents a resizable circular array of integers. Any object of this class encapsulates an ordinary array of integers as well as an index of the starting position. Elements can be added to the array which grows as necessary. The first and last element of the array can be removed in constant time while removing any other element requires a linear time shift of the other elements in the array.


Constructor Summary
CircularIntArray()
          Constructs a new circular array of integers with a default initial capacity of 5 elements.
CircularIntArray(int initialCapacity)
          Constructs a new array of integers with the given initial capacity.
 
Method Summary
 void add(int x)
          Adds the element x at the end of this circular array.
 void clear()
          Clears this circular array.
 CircularIntArray clone()
          Returns a copy of this circular array of integers.
 int get(int i)
          Returns the value of element i in this circular array.
 void init(CircularIntArray a)
          Initializes this circular array of integers with the contents of another circular array a.
static void main(String[] args)
           
 int remove(int i)
          Removes and returns the element with index i in the array.
 int removeFirst()
          Removes and returns the first element of this circular array.
 int removeLast()
          Removes and returns the last element of this circular array.
 void set(int i, int e)
          Sets the value of element i to e.
 int size()
          Returns the size of this circular array.
 String toString()
          Constructs and returns a string representation of the form [e1, e2, ...] of this array.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CircularIntArray

public CircularIntArray()
Constructs a new circular array of integers with a default initial capacity of 5 elements.


CircularIntArray

public CircularIntArray(int initialCapacity)
Constructs a new array of integers with the given initial capacity.

Parameters:
initialCapacity - the initial capacity of the array.
Method Detail

clear

public void clear()
Clears this circular array. After a call to this method, the size of the array returned by size() is 0.


init

public void init(CircularIntArray a)
Initializes this circular array of integers with the contents of another circular array a. This method replaces the internal array of this object with a clone of the internal array of a, and also copies the size and starting position of the given array.

Parameters:
a - the other circular array to copy.

add

public void add(int x)
Adds the element x at the end of this circular array. If adding the element would exceed the capacity of the internal array, the internal array gorows.

Parameters:
x - the new element to add.

removeFirst

public int removeFirst()
Removes and returns the first element of this circular array. If the size of the array returned by the size() method is 0, this method throws a NoSuchElementException.

Returns:
the value of the (removed) first element.

removeLast

public int removeLast()
Removes and returns the last element of this circular array. If the size of the array returned by the size() method is 0, this method throws a NoSuchElementException.

Returns:
the value of the (removed) last element.

size

public int size()
Returns the size of this circular array.

Returns:
the size of the circular array.

get

public int get(int i)
Returns the value of element i in this circular array.

Parameters:
i - the index of the queried element.
Returns:
the value of the element.

set

public void set(int i,
                int e)
Sets the value of element i to e.

Parameters:
i - the index of the element to modify.
e - the new value of the element.

remove

public int remove(int i)
Removes and returns the element with index i in the array. If the given index is 0, this calls removeFirst(). If the given index is size() minus 1, this returns the result of removeLast(). Otherwise, elements of the array are shifted appropriately to remove the element.

Parameters:
i - the index of the element to remove.
Returns:
the removed element.

clone

public CircularIntArray clone()
Returns a copy of this circular array of integers. The returned copy is completely independent from this instance since the internal array is also cloned.

Overrides:
clone in class Object

toString

public String toString()
Constructs and returns a string representation of the form [e1, e2, ...] of this array.

Overrides:
toString in class Object

main

public static void main(String[] args)

ContactCenters
V. 0.9.9.

To submit a bug or ask questions, send an e-mail to Richard Simard.