Lecture 12 - Arrays

Lecture 12 - Arrays - Arrays Introduction In Java, arrays...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Arrays Introduction In Java, arrays are treated much like objects, with some restrictions: o Manipulated by reference o Created dynamically at run time with the new operator o Garbage collected when no longer referred to o Is an implicit subclass of the class Object o Cannot be subclassed One-Dimensional Arrays Are declared and created using the general form: dataType[] arrayName = new dataType[size] Eg: int[] = myArray = new int[5]; This is equivalent to: int myArray[] = new int[5]; Or: int[] myArray; myArray = new int[5]; The length of the array is set once created using new o Cannot be changed o Is available using the length field Eg: myArray.length Array elements are numbered 0 to length -1, and are accessed using an array index o Form: arrayName[index] o EG: for (int i = 0; i < myArray.length; i++) System.out.println(i + “= “ + myArray[i]);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
If an array index is ever out of range, an IndexOutOfBoundsException is thrown Arrays can be initialized when created using an
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/04/2010 for the course CPSC 233 taught by Professor Li during the Fall '09 term at University of Calgary.

Page1 / 4

Lecture 12 - Arrays - Arrays Introduction In Java, arrays...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online