Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.

Similar presentations


Presentation on theme: "Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells."— Presentation transcript:

1 Lec 19 Array Intro

2 Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells

3 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 exampleexample – Arrays are objects, but its elements may be primitives or objects (depending on the array)

4 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;

5 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];

6 Combined declaration and construction double[] testScores = new double[25];

7 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!

8 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)

9 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);

10 A real-life example – See example of an instantiable class that uses an array of Particle objectsexampleParticle

11 Memory diagrams of arrays (weakly covered in section 7.1 in book) – Work out an example and see output and memory diagramexampleoutputmemory 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)

12 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 ppt "Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells."

Similar presentations


Ads by Google