Lecture 5 Arrays A way to organize data



![Declaring an Array Variable n Array declarations use square brackets. datatype[] label; n For Declaring an Array Variable n Array declarations use square brackets. datatype[] label; n For](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-4.jpg)
![Creating a New "Empty" Array Use this syntax: new int[20] n The new keyword Creating a New "Empty" Array Use this syntax: new int[20] n The new keyword](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-5.jpg)


![Filling an Array n Assign values to compartments: prices[0] = 6. 75; prices[1] = Filling an Array n Assign values to compartments: prices[0] = 6. 75; prices[1] =](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-8.jpg)


![Length of array String[] names = { "David", "Qian", "Emina", "Jamal", "Ashenafi" }; int Length of array String[] names = { "David", "Qian", "Emina", "Jamal", "Ashenafi" }; int](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-11.jpg)
![Example String[] names = { "Aisha", "Tamara", "Gikandi", "Ato", "Lauri"}; for(int i = 0; Example String[] names = { "Aisha", "Tamara", "Gikandi", "Ato", "Lauri"}; for(int i = 0;](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-12.jpg)
![Modifying Array Elements n Example: names[0] = “Bekele" n Now the first name in Modifying Array Elements n Example: names[0] = “Bekele" n Now the first name in](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-13.jpg)
![Example int[] fibs = new int[10]; fibs[0] = 1; fibs[1] = 1; for(int i Example int[] fibs = new int[10]; fibs[0] = 1; fibs[1] = 1; for(int i](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-14.jpg)

![Exercise 2 n Given this code fragment, int[] data = new int[10]; System. out. Exercise 2 n Given this code fragment, int[] data = new int[10]; System. out.](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-16.jpg)




- Slides: 20

Lecture 5 Arrays A way to organize data MIT AITI 2004

What are Arrays? n An array is a series of compartments to store data. n Each compartment is appropriately sized for the particular data type the array is declared to store. An array can hold only one type of data! E. g. int[] can hold only integers char[] can hold only characters n

Array Visualization Specifies an array of variables of type int We are creating a new array object int[] primes = new int[10]; The name of the array // An array of 10 integers The array object is of type int and has ten elements primes[0] primes[1] primes[2] primes[3] primes[4] index values primes[9]
![Declaring an Array Variable n Array declarations use square brackets datatype label n For Declaring an Array Variable n Array declarations use square brackets. datatype[] label; n For](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-4.jpg)
Declaring an Array Variable n Array declarations use square brackets. datatype[] label; n For example: int[] prices; String[] names;
![Creating a New Empty Array Use this syntax new int20 n The new keyword Creating a New "Empty" Array Use this syntax: new int[20] n The new keyword](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-5.jpg)
Creating a New "Empty" Array Use this syntax: new int[20] n The new keyword creates an array of type int that has 20 compartments n The new array can then be assigned to an array variable: int[] prices = new int[20]; n When first created as above, the items in the array are initialized to the zero value of the datatype int: 0 double: 0. 0 String: null

Array Indexes n Every compartment in an array is assigned an integer reference. n This number is called the index of the compartment n Important: In Java (and most other languages), the index starts from 0 and ends at n-1, where n is the size of the array

Accessing Array Elements n To access an item in an array, type the name of the array followed by the item’s index in square brackets. n For example, the expression: names[0] will return the first element in the names array
![Filling an Array n Assign values to compartments prices0 6 75 prices1 Filling an Array n Assign values to compartments: prices[0] = 6. 75; prices[1] =](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-8.jpg)
Filling an Array n Assign values to compartments: prices[0] = 6. 75; prices[1] = 80. 43; prices[2] = 10. 02;

Constructing Arrays § To construct an array, you can declare a new empty array and then assign values to each of the compartments: String[] names[0] names[1] names[2] names[3] names[4] names = new String[5]; = "David"; = "Qian"; = "Emina"; = "Jamal"; = "Ashenafi";

Another Way to Construct Arrays n You can also specify all of the items in an array at its creation. n Use curly brackets to surround the array’s data and separate the values with commas: String[] names = { "David", "Qian", "Emina", "Jamal", "Ashenafi"}; n Note that all the items must be of the same type. Here they are of type String. n Another example: int[] powers = {0, 1, 100};
![Length of array String names David Qian Emina Jamal Ashenafi int Length of array String[] names = { "David", "Qian", "Emina", "Jamal", "Ashenafi" }; int](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-11.jpg)
Length of array String[] names = { "David", "Qian", "Emina", "Jamal", "Ashenafi" }; int number. Of. Names = names. length; System. out. println(number. Of. Names); Output: 5 n Important: Arrays are always of the same size: their lengths cannot be changed once they are created!
![Example String names Aisha Tamara Gikandi Ato Lauri forint i 0 Example String[] names = { "Aisha", "Tamara", "Gikandi", "Ato", "Lauri"}; for(int i = 0;](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-12.jpg)
Example String[] names = { "Aisha", "Tamara", "Gikandi", "Ato", "Lauri"}; for(int i = 0; i < names. length; i++){ System. out. println("Hello " + names[i] + ". "); } Output: Hello Hello Aisha. Tamara. Gikandi. Ato. Lauri.
![Modifying Array Elements n Example names0 Bekele n Now the first name in Modifying Array Elements n Example: names[0] = “Bekele" n Now the first name in](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-13.jpg)
Modifying Array Elements n Example: names[0] = “Bekele" n Now the first name in names[] has been changed from "Aisha" to "Bekele". n So the expression names[0] now evaluates to "Bekele". n Note: The values of compartments can change, but no new compartments may be added.
![Example int fibs new int10 fibs0 1 fibs1 1 forint i Example int[] fibs = new int[10]; fibs[0] = 1; fibs[1] = 1; for(int i](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-14.jpg)
Example int[] fibs = new int[10]; fibs[0] = 1; fibs[1] = 1; for(int i = 2; i < fibs. length; i++) { fibs[i] = fibs[i-2] + fibs[i-1]; } Note: array indexes can be expressions n After running this code, the array fibs[] contains the first ten Fibonacci numbers: 1 1 2 3 5 8 13 21 34 55

Exercise 1 n Which of the following sequences of statements does not create a new array? a. int[] arr = new int[4]; b. int[] arr; arr = new int[4]; c. int[] arr = { 1, 2, 3, 4}; d. int[] arr; just declares an array variable
![Exercise 2 n Given this code fragment int data new int10 System out Exercise 2 n Given this code fragment, int[] data = new int[10]; System. out.](https://slidetodoc.com/presentation_image_h2/3c07f27f189dc1b4e5820a64d24f338d/image-16.jpg)
Exercise 2 n Given this code fragment, int[] data = new int[10]; System. out. println(data[j]); n Which of the following is a legal value of j? a. -1 b. 0 c. 3. 5 d. 10 // // out of range legal value out of range

Exercise 3 n Which set of data would not be suitable for storing in an array? a. b. c. d. the score for each of the four quarters of a Football match your name, date of birth, and score on your physics//test these are different types temperature readings taken every hour throughout a day your expenses each month for an entire year

Exercise 4 n What is the value of c after the following code segment? int int for [] a [] b [] c (int c[j] = = = j = {1, 2, 3, 4, 5}; {11, 12, 13}; new int[4]; = 0; j < 3; j++) { a[j] + b[j]; c = [12, 14, 16, 0] }

2 -Dimensional Arrays n n The arrays we've used so far can be thought of as a single row of values. A 2 -dimensional array can be thought of as a grid (or matrix) of values 0 0 8 1 4 1 9 7 2 3 6 n Each element of the 2 -D array is accessed by providing two indexes: a row index and a column index n (A 2 -D array is actually just an array of arrays) value at row index 2, column index 0 is 3

2 -D Array Example n Example: A landscape grid of a 20 x 55 acre piece of land: We want to store the height of the land at each row and each column of the grid. n We declare a 2 D array two sets of square brackets: double[][] heights = new double[20][55]; n This 2 D array has 20 rows and 55 columns n To access the acre at row index 11 and column index 23 user: heights[11][23]