Array Basics Suppose you need to process daily

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.

Arrays Variables are Reference Variables Data Type reference primitive long byte int String short char float Message. Box Applet double Hi. Lo boolean Primitive variables contain values Reference variables point at objects Input. Box etc.

Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 0 1 2 3 The index of the first position in an array is 0. 4 5 6 7 8 9 10 11
![Arrays of Primitive Data Types Array Declaration <data type> [ ] <variable> <data type> Arrays of Primitive Data Types Array Declaration <data type> [ ] <variable> <data type>](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-4.jpg)
Arrays of Primitive Data Types Array Declaration <data type> [ ] <variable> <data type> <variable>[ ] //variation 1 //variation 2 Array Creation <variable> Example = new <data type> [ <size> ] Variation 1 Variation 2 double[] rainfall; double rainfall [ ]; rainfall = new double[12]; An array is like an object!
![Accessing Individual Elements Individual elements in an array accessed with the indexed expression. double[] Accessing Individual Elements Individual elements in an array accessed with the indexed expression. double[]](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-5.jpg)
Accessing Individual Elements Individual elements in an array accessed with the indexed expression. double[] rainfall = new double[12]; rainfall 0 1 2 3 4 5 6 rainfall[2] 7 8 9 10 This indexed expression refers to the element at position #2 11

Example Programs Zadoot … out to reality … Array. Elements 1. java Zadoot … out to reality … Array. Elements 2. java
![Array Lengths double[] rainfall = new double[12]; double annual. Average; double sum = 0. Array Lengths double[] rainfall = new double[12]; double annual. Average; double sum = 0.](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-7.jpg)
Array Lengths double[] rainfall = new double[12]; double annual. Average; double sum = 0. 0; int index; The public constant length returns the capacity of an array. for (index = 0; index < rainfall. length; index++) { rainfall[index] = keyboard. next. Double(); sum += rainfall[index]; } annual. Average = sum / rainfall. length; Phrrud … out to reality … Array. Rain. java Phrrud … out to reality … Array. Averages. java

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 … Array. Bounds. Error. java

Array Initialization Like other data types, arrays can be declared and initialized at the same time. int[] number = { 2, 4, 6, 8 }; double[] sampling. Data = { 2. 443, 8. 99, 12. 3, 45. 009, 18. 2, 9. 00, 3. 123, 22. 084, 18. 08 }; The capacity of the array is set to the number of elements in the list. number. length 4 sampling. Data. length 9 Groeeet … out to reality … Array. Init. java (Secretly, Java pre-initializes arrays to the default value for the type, e. g. , 0 for int arrays. )

References are Pointers A reference variable points to an object So, arrays are like objects, but don't worry about that for now – we’ll come back to it. But it does mean you can Lose an array Have an array variable pointing nowhere Have multiple references to an array Not copy an array with =
![Losing an Array customer Array in Memory A int[] customer; B A. The variable Losing an Array customer Array in Memory A int[] customer; B A. The variable](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-11.jpg)
Losing an Array customer Array in Memory A int[] customer; B A. The variable is allocated in memory. customer = new int[5]; B. The reference to the customer = new int[5]; new array is assigned to customer C Code Array in Memory C. The reference to another array overwrites the reference in customer. State of Memory

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

The array to no-where An array variable can be explicitly made to point to no data, using the null value A null array is different from an array of length zero. Spaaocie … out to reality … Array. NULL. java
![Having Two References to an Array clemens twain A int[] clemens, twain; Array in Having Two References to an Array clemens twain A int[] clemens, twain; Array in](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-14.jpg)
Having Two References to an Array clemens twain A int[] clemens, twain; Array in Memory B clemens = new int[5]; twain allocated in memory. B. The reference to the = clemens; new array is assigned to clemens C Code A. Variables are C. The reference in clemens is assigned to customer. State of Memory Dooop … out to reality … Array. Dup. java

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 … Array. Clone. java

Two-Dimensional Arrays Two-dimensional arrays are useful in representing tabular information.
![Declaring and Creating a 2 -D Array Declaration <data type> [][] <variable> <data type> Declaring and Creating a 2 -D Array Declaration <data type> [][] <variable> <data type>](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-17.jpg)
Declaring and Creating a 2 -D Array Declaration <data type> [][] <variable> <data type> <variable>[][] //variation 1 //variation 2 Creation <variable> = new <data type> [<size 1>][<size 2>] Example double[][] pay. Scale. Table; pay. Scale. Table = new double[4][5]; 0 0 1 2 3 4

Example Programs Ieeei … out to reality … Array. Matrix. java Ieeei … out to reality … Array. Calendar. java Ieeei … out to reality … Array. Cube. java

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[0] data[1] data[2] data = new int[3][]; = new int[5];
![Multi-dimensional Arrays in RAM int[][] data = new int[3][5]; Zuuu … out to reality Multi-dimensional Arrays in RAM int[][] data = new int[3][5]; Zuuu … out to reality](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-20.jpg)
Multi-dimensional Arrays in RAM int[][] data = new int[3][5]; Zuuu … out to reality … Array. RAMMatrix. java Zuuu … out to reality … Array. RAMCube. java
![Irregular Arrays int[][] weird. Data = new int[3][]; weird. Data[0] = new int[5]; weird. Irregular Arrays int[][] weird. Data = new int[3][]; weird. Data[0] = new int[5]; weird.](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-21.jpg)
Irregular Arrays int[][] weird. Data = new int[3][]; weird. Data[0] = new int[5]; weird. Data[1] = new int[4]; weird. Data[2] = new int[7];
![Irregular Arrays int[][] weird. Data = new int[3][]; weird. Data[0] = new int[5]; weird. Irregular Arrays int[][] weird. Data = new int[3][]; weird. Data[0] = new int[5]; weird.](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-22.jpg)
Irregular Arrays int[][] weird. Data = new int[3][]; weird. Data[0] = new int[5]; weird. Data[1] = new int[4]; weird. Data[2] = new int[7]; weird. Data. length == 3 weird. Data[1]. length == 4 Jioooul … out to reality … Array. Irreg 1. java
![Irregular Arrays … null int[][] data = new int[3][]; data[0] = new int[5]; data[1] Irregular Arrays … null int[][] data = new int[3][]; data[0] = new int[5]; data[1]](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-23.jpg)
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 … Array. Irreg 2. java

Multi-dimensional Array Initialization Like 1 D arrays, it is possible to declare and initialize a multi-dimensional array at the same time. Frong … out to reality … Array. MDInit. java Frong … out to reality … Array. Cube. Init. java
![Passing Arrays to Methods - 1 Code A public int search. Minimum(float[] number)) { Passing Arrays to Methods - 1 Code A public int search. Minimum(float[] number)) {](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-25.jpg)
Passing Arrays to Methods - 1 Code A public int search. Minimum(float[] number)) { min. One = search. Minimum(array. One); … } At A before search. Minimum array. One A. Local variable number does not exist before the method execution State of Memory
![Passing Arrays to Methods - 2 Code public int search. Minimum(float[] number)) { B Passing Arrays to Methods - 2 Code public int search. Minimum(float[] number)) { B](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-26.jpg)
Passing Arrays to Methods - 2 Code public int search. Minimum(float[] number)) { B min. One = search. Minimum(array. One); … } The address is copied at B array. One number B. The value of the argument, which is an address, is copied to the parameter. State of Memory
![Passing Arrays to Methods - 3 Code public int search. Minimum(float[] number)) { min. Passing Arrays to Methods - 3 Code public int search. Minimum(float[] number)) { min.](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-27.jpg)
Passing Arrays to Methods - 3 Code public int search. Minimum(float[] number)) { min. One = search. Minimum(array. One); … C } While at C array. One inside the method number C. The array is accessed via number inside the method. State of Memory
![Passing Arrays to Methods - 4 Code public int search. Minimum(float[] number)) { min. Passing Arrays to Methods - 4 Code public int search. Minimum(float[] number)) { min.](http://slidetodoc.com/presentation_image_h2/e005f5be66b9d2f07763a6f1bb6f90b4/image-28.jpg)
Passing Arrays to Methods - 4 Code public int search. Minimum(float[] number)) { min. One = search. Minimum(array. One); … D } At D after search. Minimum array. One number D. The parameter is erased. The argument still points to the same object. State of Memory

Example Programs Flunot … out to reality … Array. Param. Avg. java Flunot … out to reality … Array. Param 1. java

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 … Array. Param 2. java

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 … Array. Local. GC. java
- Slides: 31