Title: Primitive Arrays
1Primitive Arrays
- A primitive array stores multiple values of the
same primitive data type.
The index of the first position in an array is 0.
2Accessing Individual Elements
- Individual elements in an array accessed with the
indexed expression.
3Array Lengths
4Example Programs
- Phrrud out to reality ArrayRain.java
- Phrrud out to reality ArrayAverages.java
5Array 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
6Array Initialization
- Like other data types, it is possible to declare
and initialize an array at the same time.
- The capacity of the array is set to the number of
elements in the list.
7Example 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
8References 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
9Having Two References to an Array
State of Memory
Code
10Example Program
- Dooop out to reality ArrayDup.java
11Cloning 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
12Losing an Array
State of Memory
Code
13Garbage Collection
- An array that has no references is garbage
collected by the java program - Spaaocie out to reality ArrayGC.java
14Two-Dimensional Arrays
- Two-dimensional arrays are useful in representing
tabular information.
15Example Programs
- Ieeei out to reality ArrayMatrix.java
- Ieeei out to reality ArrayCalendar.java
- Ieeei out to reality ArrayCube.java
16Multi-dimensional Arrays NOT
- Java does not really have multi-dimensional
arrays - Java has arrays of arrays
- int data new int35
- is shorthand for
- int data new int3
- data0 new int5
- data1 new int5
- data2 new int5
17Multi-dimensional Arrays in RAM
- Zuuu out to reality ArrayRAM.java
18Irregular Arrays
- int weirdData new int3
- weirdData0 new int5
- weirdData1 new int4
- weirdData2 new int7
-
19Irregular Arrays
- int weirdData new int3
- weirdData0 new int5
- weirdData1 new int4
- weirdData2 new int7
- weirdData.length 3
- weirdData1.length 4
- Jioooul out to reality ArrayIrreg1.java
20Passing Arrays to Methods - 1
Code
public int searchMinimum(float
number))
A
minOne searchMinimum(arrayOne)
A. Local variable number does not exist before
the method execution
State of Memory
21Passing Arrays to Methods - 2
Code
public int searchMinimum(float
number))
minOne searchMinimum(arrayOne)
B. The value of the argument, which is an
address, is copied to the parameter.
State of Memory
22Passing Arrays to Methods - 3
Code
public int searchMinimum(float
number))
minOne searchMinimum(arrayOne)
C. The array is accessed via number inside the
method.
State of Memory
23Passing Arrays to Methods - 4
Code
public int searchMinimum(float
number))
minOne searchMinimum(arrayOne)
D. The parameter is erased. The argument still
points to the same object.
State of Memory
24Example Programs
- Flunot out to reality ArrayParamAvg.java
- Flunot out to reality ArrayParam1.java
25Returning 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
26Local 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