Simple arrays

advertisement

Lec 19

Array Intro

Agenda

• Arrays—what are they

• Declaring Arrays

• Constructing Arrays

• Accessing Array cells

Arrays (section 7.1 in book)

– An array is a list of variables

• The variables within are called the elements of the array

• Use the elements of the array as you would any other variable (same rules for primitive vs reference, assignment, and usage)

– See example

– Arrays are objects, but its elements may be primitives or objects (depending on the array)

Declaring Arrays

//declares an array variable called "a" which can hold a

//reference to an array of boolean values boolean[] a;

//declares a array variables "d" which can hold a

//reference to an array of double values double[] d;

//declares an array variable called "firework" which can hold a

//reference to an array of Particle objects

Particle[] firework;

Constructing Arrays

//creates a new array with 5 boolean values and sets "a" to a

//reference to that array. All the values default to false.

a = new boolean[5];

//creates a new array with 10 double values and sets "d" to a

//reference to that array. All the values default to 0.0

d = new double[10];

//Creates a new array with 100 Paritcle values, all of which

//default to null. Then sets firework with a reference to

//that array.

//Note that this statement DOESN'T create any Particle objects!

firework = new Particle[100];

Combined declaration and construction

double[] testScores = new double[25];

Storing and Retrieving array data

//note that we index starting from zero testScores[0] = 68.2;

//this would be the last element of the array; since there are

//25 elements it goes from 0 through 24.

testScores[24] = 72.4;

System.out.println(testScores[0]); //prints 68.2

System.out.println(testScores[24]); //prints 72.4

System.out.println(testScores[23]); //prints 0.0

//System.out.println(testScores[25]); //BAD! Runtime error!

Main things to know with arrays (section 7.1 in book)

– Use the datatype[] varName syntax to declare a variable capable of holding an array

• For example: int[] myArray;

– Use the new datatype[length] syntax to construct a new array object

• For example: myArray = new int[5];

• The length of an array is fixed when it is created

– The elements of the array are initialized to their usual defaults (0 if using number primitives, false if using boolean, null if using objects)

Main things to know with arrays (continued)

– Refer to elements of the array using arrayName[index]

• For example: System.out.println(myArray[3]);

• Use and assignment of array elements work just like the usual variables of that type

– The index to an array starts counting from zero

• So an array of length 3 has elements at indices 0, 1, and 2

– You can access the length of an array by using the public

length instance variable

• For example: System.out.println(myArray.length);

A real-life example

– See example of an instantiable class that uses an array of Particle objects

Memory diagrams of arrays (weakly covered in section 7.1 in book)

– Work out an example and see output and memory diagram

– Arrays are objects and therefore variables that hold arrays are reference variables and work via references (arrows)

– The elements of an array might be primitives or objects depending on what type of array it is. In either case, the usual rules apply (references for objects, actual values for primitives)

Lab19 Using Arrays

• You'll be creating an applet that uses arrays to store circle radii, the points of a triangle, and a list of lines from a poem:

Download