Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Array Basics Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can,

Similar presentations


Presentation on theme: "Chapter 9 Array Basics Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can,"— Presentation transcript:

1 Chapter 9 Array Basics Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can, but would you? An array is a collection of data values of the same data type. If your program needs to deal with 100 integers, 365 real numbers, etc., you will use an array. Intro to OOP w/Java--Wu

2 Chapter 9 Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 1 2 3 4 5 6 7 8 9 10 11 The index of the first position in an array is 0. Intro to OOP w/Java--Wu

3 Arrays Variables are Reference Variables
Chapter 9 Arrays Variables are Reference Variables byte short int double long float boolean String Applet MessageBox HiLo InputBox etc. char primitive reference Data Type A constant data value is represented graphically by a lock icon to convey the fact that the value is locked and cannot be changed. Primitive variables contain values Reference variables point at objects Intro to OOP w/Java--Wu

4 Arrays of Primitive Data Types
Chapter 9 Arrays of Primitive Data Types Array Declaration <data type> [ ] <variable> //variation 1 <data type> <variable>[ ] //variation 2 Array Creation <variable> = new <data type> [ <size> ] Example double[] rainfall; rainfall = new double[12]; Variation 1 double rainfall [ ]; rainfall = new double[12]; Variation 2 As you can declare and create objects in one statement such as Person p = new Person( ); you can declare and create an array in one statement as double[ ] rainfall = new double[12]; Strictly speaking, an array is a reference data type and really an object because there is no class called Array in Java. The thumbnail note in page 413 is therefore not 100 percent accurate. An array is like an object! Intro to OOP w/Java--Wu

5 Accessing Individual Elements
Chapter 9 Accessing Individual Elements Individual elements in an array accessed with the indexed expression. double[] rainfall = new double[12]; rainfall 1 2 3 4 5 6 7 8 9 10 11 rainfall[2] This indexed expression refers to the element at position #2 Intro to OOP w/Java--Wu

6 Example Programs Zadoot … out to reality … ArrayElements1.java

7 Array Lengths double[] rainfall = new double[12];
Chapter 9 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. Notice the public constant length returns the capacity, not the actual number of non-blank values in the array. Intro to OOP w/Java--Wu

8 Example Programs Phrrud … out to reality … ArrayRain.java
Phrrud … out to reality … ArrayAverages.java

9 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

10 Chapter 9 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, , 18.2, 9.00, 3.123, , }; 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.length samplingData.length monthName.length 4 9 12 Intro to OOP w/Java--Wu

11 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

12 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

13 Having Two References to an Array
Chapter 9 Having Two References to an Array A. Variables are allocated in memory. clemens twain B. The reference to the new array is assigned to clemens. Array in Memory C. The reference in clemens is assigned to customer. A int[] clemens, twain; B clemens = new int[5]; C twain = clemens; Code State of Memory Intro to OOP w/Java--Wu

14 Example Program Dooop … out to reality … ArrayDup.java

15 Cloning an Array An array can be copied using the clone method
Chapter 9 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 Intro to OOP w/Java--Wu

16 Losing an Array customer A B C Code State of Memory
Chapter 9 Losing an Array customer A. The variable is allocated in memory. B. The reference to the new array is assigned to customer. Array in Memory C. The reference to another array overwrites the reference in customer. Array in Memory A int[] customer; B customer = new int[5]; C customer = new int[5]; Code State of Memory Intro to OOP w/Java--Wu

17 Garbage Collection An array that has no references is garbage collected by the java program Spaaocie … out to reality … ArrayGC.java

18 Two-Dimensional Arrays
Chapter 9 Two-Dimensional Arrays Two-dimensional arrays are useful in representing tabular information. Intro to OOP w/Java--Wu

19 Declaring and Creating a 2-D Array
Chapter 9 Declaring and Creating a 2-D Array Declaration <data type> [][] <variable> //variation 1 <data type> <variable>[][] //variation 2 Creation <variable> = new <data type> [<size1>][<size2>] Example 1 2 3 4 payScaleTable = new double[4][5]; is really a shorthand for payScaleTable = new double[4][ ]; payScaleTable[0] = new double[5]; payScaleTable[1] = new double[5]; payScaleTable[2] = new double[5]; payScaleTable[3] = new double[5]; which is equivalent to for (int i = 0; i < 4; i++) { payScaleTable[i] = new double[5]; } double[][] payScaleTable; payScaleTable = new double[4][5]; 1 2 3 Intro to OOP w/Java--Wu

20 Example Programs Ieeei … out to reality … ArrayMatrix.java
Ieeei … out to reality … ArrayCalendar.java Ieeei … out to reality … ArrayCube.java

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

22 Multi-dimensional Arrays in RAM
int[][] data = new int[3][5]; Zuuu … out to reality … ArrayRAM.java

23 Irregular Arrays int[][] weirdData = new int[3][];

24 Irregular Arrays int[][] weirdData = new int[3][];
weirdData.length == 3 weirdData[1].length == 4 Jioooul … out to reality … ArrayIrreg1.java

25 Irregular Arrays … null
int[][] data = new int[3][]; data[0] = new int[5]; data[1] = new int[4]; data[2] = null; Jioooul … out to reality … ArrayIrreg2.java

26 Multi-dimensional Array Initialization
Chapter 9 Multi-dimensional Array Initialization Like 1D arrays, it is possible to declare and initialize a multi-dimensional array at the same time. Frong … out to reality … ArrayCubeInit.java Frong … out to reality … ArrayMDInit.java Intro to OOP w/Java--Wu

27 Passing Arrays to Methods - 1
Chapter 9 Passing Arrays to Methods - 1 Code A public int searchMinimum(float[] number)) { } minOne = searchMinimum(arrayOne); At before searchMinimum A arrayOne A. Local variable number does not exist before the method execution When an array is passed to a method, only its reference is passed. A copy of the array is NOT created in the method. public int searchMinimum(float[] number) { int indexOfMinimum = 0; for (int i = 1; i < number.length; i++) { if (number[i] < number[indexOfMinimum]) { //found a indexOfMinimum = i; //smaller element } return indexOfMinimum; State of Memory Intro to OOP w/Java--Wu

28 Passing Arrays to Methods - 2
Chapter 9 Passing Arrays to Methods - 2 Code public int searchMinimum(float[] number)) { } B minOne = searchMinimum(arrayOne); arrayOne The address is copied at B number arrayOne B. The value of the argument, which is an address, is copied to the parameter. State of Memory Intro to OOP w/Java--Wu

29 Passing Arrays to Methods - 3
Chapter 9 Passing Arrays to Methods - 3 Code public int searchMinimum(float[] number)) { } C minOne = searchMinimum(arrayOne); arrayOne number While at inside the method C C. The array is accessed via number inside the method. State of Memory Intro to OOP w/Java--Wu

30 Passing Arrays to Methods - 4
Chapter 9 Passing Arrays to Methods - 4 Code public int searchMinimum(float[] number)) { } minOne = searchMinimum(arrayOne); D arrayOne At after searchMinimum D arrayOne number D. The parameter is erased. The argument still points to the same object. State of Memory Intro to OOP w/Java--Wu

31 Example Programs Flunot … out to reality … ArrayParamAvg.java
Flunot … out to reality … ArrayParam1.java

32 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

33 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


Download ppt "Chapter 9 Array Basics Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can,"

Similar presentations


Ads by Google