primitive arrays a primitive array stores multiple values of the same primitive data type. rainfall...

26
Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 0 1 2 3 4 5 6 7 8 9 10 11 The index of the first position in an array is 0.

Upload: gladys-robinson

Post on 20-Jan-2016

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Primitive Arrays

A primitive array stores multiple values of the same primitive data type.

rainfall0 1 2 3 4 5 6 7 8 9 10 11

The index of the firstposition in an array is 0.

Page 2: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Accessing Individual Elements

Individual elements in an array accessed with the indexed expression.

double[] rainfall = new double[12];

rainfall0 1 2 3 4 5 6 7 8 9 10 11

rainfall[2]This indexed expression refers to the element at position #2

This indexed expression refers to the element at position #2

Page 3: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Array Lengths

double[] rainfall = new double[12];

double annualAverage;

double sum = 0.0;

int index;

for (index = 0; index < rainfall.length; index++) {

rainfall[index] = SavitchIn.readLineDouble();

sum += rainfall[index];

}

annualAverage = sum / rainfall.length;

The public constant length returns the capacity of an array.

The public constant length returns the capacity of an array.

Page 4: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Example Programs

Phrrud … out to reality … ArrayRain.java

Phrrud … out to reality … ArrayAverages.java

Page 5: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Array Bounds Errors

Trying to access an array element that does not exist causes a runtime error

Negative indices

Indices beyond the size

Falop … out to reality … ArrayBoundsError.java

Page 6: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Array Initialization

Like other data types, it is possible to declare and initialize an array at the same time.

int[] number = { 2, 4, 6, 8 };

double[] samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2, 9.00, 3.123, 22.084, 18.08 };

String[] monthName = { "January", "February", "March", "April", "May", "June", "July",

"August", "September", "October","November", "December" };

The capacity of the array is set to the number of elements in the list.

number.lengthsamplingData.length monthName.length

4 912

Page 7: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Example Programs

Groeeet … out to reality … ArrayInit.java

An array variable can be explicitly made to point to no data, using the null value

Spaaocie … out to reality … ArrayNULL.java

Page 8: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

References are Pointers

A reference variable points to an object

So, arrays are objects, but don't worry about that now

But it does mean you can

Have multiple references to an array

Not copy an array with =

Lose an array

Page 9: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Having Two References to an Array

Code State of Memory

AA

int[] clemens, twain;int[] clemens, twain; BB

clemens = new int[5];clemens = new int[5];

CC

twain = clemens;twain = clemens;

A. A. Variables are allocated in memory.

A. A. Variables are allocated in memory.

clemens

twain

B. B. The reference to the new array is assigned to clemensclemens.

B. B. The reference to the new array is assigned to clemensclemens.

Arrayin

Memory

Arrayin

Memory

C. C. The reference in clemensclemens is assigned to

customer.customer.

C. C. The reference in clemensclemens is assigned to

customer.customer.

Page 10: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Example Program

Dooop … out to reality … ArrayDup.java

Page 11: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Cloning an Array

An array can be copied using the clone method

It's necessary to cast the clone to the right array type

Babababoom … out to reality … ArrayClone.java

Page 12: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Losing an Array

Code State of Memory

customer

A. A. The variable is allocated in memory.

A. A. The variable is allocated in memory.

AA

int[] customer;int[] customer;BB

customer = new int[5];customer = new int[5];

CC

customer = new int[5];customer = new int[5];B. B. The reference to the

new array is assigned to customercustomer.

B. B. The reference to the new array is assigned to customercustomer.

Arrayin

Memory

Arrayin

Memory

C. C. The reference to another array overwrites the reference in customer.customer.

C. C. The reference to another array overwrites the reference in customer.customer.

Arrayin

Memory

Arrayin

Memory

Page 13: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Garbage Collection

An array that has no references is garbage collected by the java program

Spaaocie … out to reality … ArrayGC.java

Page 14: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Two-Dimensional Arrays

Two-dimensional arrays are useful in representing tabular information.

Page 15: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Example Programs

Ieeei … out to reality … ArrayMatrix.java

Ieeei … out to reality … ArrayCalendar.java

Ieeei … out to reality … ArrayCube.java

Page 16: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Multi-dimensional Arrays … NOT

Java does not really have multi-dimensional arrays

Java has arrays of arrays

int[][] data = new int[3][5];

is shorthand for

int[][] data = new int[3][];data[0] = new int[5];data[1] = new int[5];data[2] = new int[5];

Page 17: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Multi-dimensional Arrays in RAM

int[][] data = new int[3][5];

Zuuu … out to reality … ArrayRAM.java

Page 18: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Irregular Arrays

int[][] weirdData = new int[3][];

weirdData[0] = new int[5];weirdData[1] = new int[4];weirdData[2] = new int[7];

Page 19: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Irregular Arrays

int[][] weirdData = new int[3][];

weirdData[0] = new int[5];weirdData[1] = new int[4];weirdData[2] = new int[7];

weirdData.length == 3

weirdData[1].length == 4

Jioooul … out to reality … ArrayIrreg1.java

Page 20: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Passing Arrays to Methods - 1

Code

State of Memory

minOne = searchMinimum(arrayOne);

public int searchMinimum(float[] number)){

}

AA

At before searchMinimumAA

arrayOneA. A. Local variable number does not exist before the method execution

A. A. Local variable number does not exist before the method execution

Page 21: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Passing Arrays to Methods - 2

Code

State of Memory

minOne = searchMinimum(arrayOne);

public int searchMinimum(float[] number)){

}

arrayOne

BB

arrayOne

The address is copied at BB

numberB. B. The value of the argument, which is an address, is copied to the parameter.

B. B. The value of the argument, which is an address, is copied to the parameter.

Page 22: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

arrayOne number

While at inside the methodCC

Passing Arrays to Methods - 3

Code

State of Memory

minOne = searchMinimum(arrayOne);

public int searchMinimum(float[] number)){

}

CC

C. C. The array is accessed via number inside the method.

C. C. The array is accessed via number inside the method.

Page 23: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

arrayOne number

Passing Arrays to Methods - 4

Code

State of Memory

minOne = searchMinimum(arrayOne);

public int searchMinimum(float[] number)){

}DD

arrayOne

At after searchMinimumDD

D. D. The parameter is erased. The argument still points to the same object.

D. D. The parameter is erased. The argument still points to the same object.

Page 24: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Example Programs

Flunot … out to reality … ArrayParamAvg.java

Flunot … out to reality … ArrayParam1.java

Page 25: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Returning Arrays

Array variables in methods exist until the method ends, but the array data lives while referenced

An array variable can be returned from a method

The receiving array variable then refers to the array data, and the array persists

Wrrbbrack … out to reality … ArrayReturn.java

Wrrbbrack … out to reality … ArrayParam2.java

Page 26: Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an

Local arrays

Array variables in methods exist until the method ends

The array data referred to by such an array variable is lost and garbage collected when the method ends

Dessserts … out to reality … ArrayLocalGC.java