View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

Declare objects but fail to initialize them

example
int[]  numArray;


this declares a reference to an array of integers but doesn't create the array. To create the array use
int[] numArray = new int[size]; // where size is how big the array should be


For a two-dimensional array use:

int[][] theArray = new int[numRow][numCols]; // the AP exam assumes row major order unless you are told otherwise


For an ArrayList of Student objects use the following. Notice the use of Generics to specify what is in the list and the use of List in the declaration of the reference. This makes it easier to use a different class that implements list in the future.

List<Student> studentList = new ArrayList<Student>(); 


Link to this Page