Professional Documents
Culture Documents
Java Fundamentals 2
Java Java
Arrays
Arrays of primitive variables Arrays of Objects Processing program arguments
T-Java
Arrays
Arrays are Java objects Array object manages a collection of: Primitive data types
Array cIndex Value
[0] [1] [2] 8 4 6
array1
reference to
Size of Array = 100 Size of Array = 100 Array values stored here Array values stored here
int[] array2; // declare a int[] array2; // declare a array2 = new int[100]; // array2 = new int[100]; // // // // declare and // declare and int[] array3 = int[] array3 =
reference to array, no object reference to array, no object size must be specified on size must be specified on instantiation instantiation
Navigate to T-Java\Exercises\exp28b
T-Java 4
array1
int array1 = new int[100]; int array1 = new int[100]; array1[0] = 1; array1[0] = 1;
The lower bound of an array is always 0 The lower bound of an array is always 0 Array bounds are automatically checked at runtime Array bounds are automatically checked at runtime
Array elements are accessed using the [ ] operator Array elements are accessed using the [ ] operator
T-Java
{ {
for (int col=0; col < myArray[row].length; col++) for (int col=0; col < myArray[row].length; col++) myArray[row][col] = row*col; myArray[row][col] = row*col; } }
} }
Navigate to T-Java\Exercises\exp28c
T-Java 6
dates
reference to
Date objects
/* /* * Store a Date object (with current time) into the array * Store a Date object (with current time) into the array */ */
Navigate to T-Java\Exercises\exp28d
T-Java 7
public static void main(String[] args) { public static void main(String[] args) { if (args.length < 2) { if (args.length < 2) { System.err.println("Usage: OrderEntry minItems maxItems"); System.err.println("Usage: OrderEntry minItems maxItems"); return; return; } }
Navigate to T-Java\Exercises\exp28e
T-Java 8
Summary
An array is a object An array is accessed by a zero-based integer index number An array has a fixed size An array of non-primitives contains references to objects The main() method may be passed run-time parameters
Key Terms Key Terms
Array Array of primitives Array of objects Date objects Program arguments Runtime parameters
T-Java