Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.

Similar presentations


Presentation on theme: "Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

2 SE15: Arrays18–2 Today’s Learning Objectives To find out what an array is Learn how to use arrays in Java programs Learn how to write methods that use arrays as parameters or return arrays Learn how to use an array as an instance variable in a class

3 SE15: Arrays18–3 Lecture Outline Array Basics Creating and accessing Arrays Arrays in Classes and Methods Case Study: Sales Report Arrays as method arguments Methods that return Arrays Savitch sections 6.1 and 6.2

4 SE15: Arrays18–4 Why do we need arrays? Consider the following code: int count; double next, sum, average; Scanner keyboard = new Scanner(System.in); System.out.println(“Enter 7 temperatures”); sum = 0; for(count=0; count < 7; count++) { next = keyboard.nextDouble(); sum = sum + next; } average = sum/7; What will happen if we wish to find the temperatures that are above the average?

5 SE15: Arrays18–5 Creating Arrays You can think of array as a collection of variables of the same type To create a collection of seven variables of type double double[] temperature = new double[7]; This will similar to declaring seven variables named temperature[0],temperature[1], temperature[2],temperature[3], temperature[4], temperature[5], temperature[6] Note the numbering starts from zero!

6 SE15: Arrays18–6 Accessing Arrays You can use these in similar ways to other variables temperature[4] = -1; temperature[6] = temperature[1] + 10; System.out.println(temperature[2]); But you can also use the number in the square brackets to compute the name of one of the variables. for( index=0; index < 7; index++) System.out.println(temperature[index]);

7 SE15: Arrays18–7 Details An array is created like an object of a class type Base_Type[] Array_Name = new Base_Type[length]; e.g. int[] pressure = new int[100]; This array has length 100 which means it has indexed variables pressure[0] through pressure[99] 3224263031.52829 0123456Indices temperature[4]

8 SE15: Arrays18–8 More Details The base type can be any type Book myBook = new Book[10]; The integer in the square brackets can be an expression that evaluates to an integer System.out.println(“ How many temperatures will be entered? ”); int size = keyboard.nextInt(); double[] temperature = new double[size]; OR int day = 1; System.out.println(“temp 3 days later” + temperature[day+3]);

9 SE15: Arrays18–9 The length instance variable An array is a kind of object and therefore may have instance variables. An array only has one public instance variable, length length is equal to the length of the array For example: Book[] myBook = new Book[20]; myBook.length has a value 20

10 SE15: Arrays18–10 Initialising Arrays An array may be initialised at the time of declaration double[] temperature = {30.0, 25.5, 32.0}; The array length is set to the minimum that will hold the values. This is equivalent to the following: double[] temperature = new double[3]; temperature[0]= 30.0; temparature[1]= 25.5; temperature[2]= 32.0; If you do not initialise the elements of an array they may be automatically initialised to a default value for the base type.

11 SE15: Arrays18–11 Arrays in Classes and Methods Case Study: Sales Report To write a program to generate sales reports for a company’s team of salesmen You need to be able to show which salesmen have the highest sales figures and how they compare the average. Need to record the name and sales figure for each salesman Use an array to keep track of the data for all salesmen and record the average and highest sales figures

12 SE15: Arrays18–12 Indexed Variables as Method Arguments An indexed variable for an array a, such as a[i], can be used anywhere that you can use any other variable of the base type. So an indexed variable can be an argument to a method in exactly the same way as any other variable of the array’s base type can be used as an argument. printBar(temperature[index]);

13 SE15: Arrays18–13 Entire Arrays as Method Arguments The way you specify an array parameter in a method definition is similar to the way you declare an array. public static double calcAverage(double[] a) { double sum = 0; int i =0; for(i=0; i < a.length; i++) { sum = sum + a[i]; } return(sum/a.length); }

14 SE15: Arrays18–14 Methods that Return Arrays In Java a method may return an array. You specify the return type for a method in the same way that you specify a type for an array parameter. public static double[] calcDiffFromAverage(double[] a) { double average = calcAverage(a); double[] diff = new double [a.length]; for(int i=0; i < diff.length; i++) { diff[i] = Math.abs(a[i] - average); } return diff; }

15 SE15: Arrays18–15 Partially Filled arrays In some situations you need some but not all of the indexed variables in an array, such as a list that is not yet full. In these situations you need to keep track of how much of the array is used. This is normally done with an int variable such as the instance variable bookCount in the Library Class which tells the methods that the array consists of the array elements 0 – bookCount-1

16 SE15: Arrays18–16 Sorting Arrays Sort an array of ints to arrange the elements so that a[0] is the smallest, a[1] is the next smallest and so forth. for (index=0; index < a.length; index++) Place the index th smallest element in a[index] Savitch 6.4

17 SE15: Arrays18–17 Selection Sort 3227242630312829 a[0]a[1]a[2]a[3]a[4]a[5]a[6]Unsorted 2427322630312829 2426322730312829 2426273230312829 2426272829303132....

18 SE15: Arrays18–18 Selection Sort for (index=0; index < a.length; index++) { // Place the correct value in a[index] indexOfNextSmallest = an index of the smallest value among a[index], a[index+1],…,a[a.length-1]; Interchange the values of a[index] and a[indexOfNextSmallest]; }

19 SE15: Arrays18–19 Summary Looked at : What arrays are How to create and initialise them How to use indexed variables How to pass and return them in methods How to sort arrays using selection sort

20 SE15: Arrays18–20 Follow-up Work Savitch chapter 6 Self Test Exercise 6 Self Test Exercise 9 Re-write the sort program to sort an Array of Strings into Alphabetical order.


Download ppt "Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google