import java.util.*; /** * An array is more convenient class of the built-in arrays. It can be used * with generic data of type D, and it is size adjusting. * * @author Rossmanith * * @param The type of the data stored in an array. */ public class Array { /* * The internal array to store this array. */ protected D[] a; /* * size-1 is the largest index for which a datum is stored in this array, * or equivalently, size is the number of elements that are stored in this array * (altough not all elements must have been stored, e.g. new Array().set(5) has size 6.) */ protected int size; /** * reallocated new space if needed */ @SuppressWarnings("unchecked") private void realloc() { if(size>=a.length) { D[] b = (D[]) new Object[2*size]; for(int i=0; i=size) { size = i+1; realloc(); } a[i] = d; } /** * Gets the i-th element of this array (in constant time). * @param i The index of element that should be returned, must be non-negative. */ public D get(int i) { if(i>=size) return null; return a[i]; } /** * Creates an empty array with capacity s. * @param s The initial capacity of the array, must be non-negative. */ @SuppressWarnings("unchecked") public Array(int s) { size = 0; a = (D[]) new Object[s]; } /** * Creates an empty array with default capacity. */ @SuppressWarnings("unchecked") public Array() { size = 0; a = (D[]) new Object[10]; } /** * Permutes this array randomly using the given * random number generator r. * @param r The random number generator. */ public void permute_randomly(Random r) { for(int i=size-1; i>0; i--) { int j=r.nextInt(i+1); D t=a[i]; a[i]=a[j]; a[j]=t; } } /** * Prints this array to stdout. */ public void print() { for(int i=0; i iterator() { return new SimpleIterator( new Iterator() { private int i = 0; public Object data() { return null; } public D key() { return Array.this.a[i]; } public boolean more() { return i < Array.this.size; } public void step() { i++; } } ); } }