You are on page 1of 9

T-Java Arrays

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

Java objects Items in an Array must all be of the same type

Array cIndex Value


[0] [1] [2]
r r r

Fixed number of items in the collection


Navigate to T-Java\Exercises\exp28a
T-Java 3

Array of Primitive Variables


int[] array1 = new int[100]; int[] array1 = new int[100]; // Array of 100 int variables // Array of 100 int variables // Initialized to default values // Initialized to default values

array1

reference to

Array cIndex Value


[0] [1] [2] 8 4 6

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

initialize initialize new int[] { 8, 6, 4, 50, 19 }; new int[] { 8, 6, 4, 50, 19 };

System.out.println(array3 size= + array3.length); System.out.println(array3 size= + array3.length);

Navigate to T-Java\Exercises\exp28b
T-Java 4

Accessing Array Elements


reference to

array1

Array cIndex Value


[0] [1] [2] 8 -4 16

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

Multidimensional Array of Primitives


Multidimensional arrays are declared by specifying additional sets of brackets, e.g. [ ] [ ] A 2-dimensional array can be visualized as a table with rows and columns
Column
0 1 2 3 4 5 0 Row 1 2 3 4 5 int myArray[][] = new int [8][6]; int myArray[][] = new int [8][6]; 6 for (int row=0; row < myArray.length; row++) { 7 for (int row=0; row < myArray.length; row++) {

0,1 3,4 5,2

{ {

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

Array of Date Objects


Date[] dates = new Date[100]; // array of 100 Date objects References to objects are stored here References to objects are stored here

dates

reference to

Array cIndex Value


[0] [1] [2]
r r r

Date objects

/* /* * Store a Date object (with current time) into the array * Store a Date object (with current time) into the array */ */

dates[i] = Calendar.getInstance().getTime(); dates[i] = Calendar.getInstance().getTime();

Navigate to T-Java\Exercises\exp28d
T-Java 7

Processing Program Arguments


The main method is passed an array of strings, each of which corresponds to a command line argument
Application name Application name Arguments Arguments

run.bat java OrderEntry 4 16

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

You might also like