Chapter 07: Arrays
Arrays are simple lists. They are the first reference data type that we will study
What is an Array?
Table of Contents
What is an Array?...........................................................................................................................................................................................................................................1
Creating and Initializing Arrays....................................................................................................................................................................................................................2
How to Change the Size of an Array?............................................................................................................................................................................................................3
Demonstration: Examine Arrays in the Debugger.......................................................................................................................................................................................4
Demonstration Summary................................................................................................................................................................................................................................5
i
What is an Array?
Almost every programming language has arrays. Arrays
private static int[] arraySyntax01() {
make sense when int i = -99;
Random r = new Random();
• you know exactly how much data you have int [] intArray = new int[4];
• you don't care if the data is in order
// initialize the array
for ( i = 0; i < [Link]; i++ ) {
Arrays are reference data types intArray[i] = [Link](20);
} // end loop
• Every element in the array is the same data type
• All elements in the array live in contiguous return intArray;
memory } // end arraySyntax01() method - -
• The number of elements in an array is fixed at private static double[] arraySyntax02() {
creation time int i = -99;
• Square brackets in a variable declaration indicate Random r = new Random();
that you are declaring an array double[] doubleArray = new double[ [Link]( 4 ) + 3 ];
• Access individual array elements by using a
for ( i = 0; i < [Link]; i++ ) {
subscript doubleArray[i] = [Link]() * [Link](200);
• Array subscripts are zero based } // end loop
• An array member named length tells how many
elements are in the array return doubleArray;
} // end arraySyntax02() - -
The Java Language Specification
by James Gosling
Bill Joy
Guy Steele
Gilad Bracha
Alex Buckley
Chapter 10 Arrays
What is an Array? 1
Creating and Initializing Arrays
There are two ways to initialize a Java array
• using (either directly or indirectly) the key word new
• using array initializer syntax
int[] array01 = new int[25];
double[] array02 = { [Link], Math.E };
Any array variable can be set to null at any time. This tells the Java Virtual Machine that the previous contents of the array are now eligible for garbage collection
array02 = null;
A word about the way the Java Virtual Machine manages memory
• The Java Virtual Machine allocates memory every time somebody's code says the key word new
• The Java Virtual Machine keeps a count of all the variables that reference that memory
• Any time that reference count drops to zero, the Java Virtual Machine reserves the option of releasing that memory
The Java Language Specification
by James Gosling
Bill Joy
Guy Steele
Gilad Bracha
Alex Buckley
section 10.2
section 10.6
Creating and Initializing Arrays 2
How to Change the Size of an Array?
In order to change the size of an array
1. create a new array sized the way you want
2. copy data from the original array to the new array
3. destroy the original array
public int[] changeIntArraySize( int[] oldArray, int newSize ) {
int[] newArray = new int[ newSize ];
for ( int i = 0, j = 0; (i < [Link]) && (j < [Link]); i++, j++ ) {
newArray[j] = oldArray[i];
} // end loop
return newArray;
} // end changeIntArraySize() - -
/* some lines taken from some other method */
int[] x = new int[4];
x[0] = 16; x[1] = -21; x[2] = 106; x[3] = -8;
x = changeIntArraySize( x, 5 );
The Java Language Specification
by James Gosling
Bill Joy
Guy Steele
Gilad Bracha
Alex Buckley
section 10.2
How to Change the Size of an Array? 3
Demonstration: Examine Arrays in the Debugger
We are going to use a debugger to step through two Java sources that illustrate the syntax of arrays
• [Link]
• [Link]
You have these files. You might want to step through them yourself after this demonstration
As part of this demonstration,
• we will watch the array creation process
• we will watch the data in each array change
• we will watch what happens when we change the size of an array
Demonstration: Examine Arrays in the Debugger 4
Demonstration Summary
In this demonstration, we have
• examined the array creation porcess
• assigned values to array elements
• used values from array elements
• examined what happens when we try to change the size of an
array
Demonstration Summary 5