FileDocCategorySizeDatePackage
Array2.javaAPI DocExample952Sun Mar 07 17:52:56 GMT 2004None

Array2

public class Array2 extends Object
Re-allocate an array, bigger...
author
Ian Darwin
version
$Id: Array2.java,v 1.6 2004/03/07 23:52:55 ian Exp $

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)

		int nDates = 0;
		final int MAX = 10;
		Calendar[] dates = new Calendar[MAX];
		Calendar c;
		StructureDemo source = new StructureDemo(21);
		while ((c=(Calendar)source.getDate()) != null) {

			// if (nDates >= dates.length) {
			// 	System.err.println("Too Many Dates! Simplify your life!!");
			// 	System.exit(1);  // wimp out
			// }

			// better: reallocate, making data structure dynamic
			if (nDates >= dates.length) {
				Calendar[] tmp = new Calendar[dates.length + 10];
				System.arraycopy(dates, 0, tmp, 0, dates.length);
				dates = tmp;    // copies the array reference
				// old array will be garbage collected soon...
			}
			dates[nDates++] = c;
		}
		System.out.println("Array size = " + dates.length);