Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16 Arrays: Part 2 COMP1681 / SE15 Introduction to Programming.

Similar presentations


Presentation on theme: "Lecture 16 Arrays: Part 2 COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 16 Arrays: Part 2 COMP1681 / SE15 Introduction to Programming

2 SE15: Arrays15–2 Today’s Learning Objectives Find out what can go wrong with Arrays 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 Introduce the topic of sorting an Array

3 SE15: Arrays15–3 Lecture Outline Recap on Arrays Creating and accessing Arrays Common problems Arrays as method arguments Methods that return Arrays Searching Arrays Sorting Arrays Savitch sections 6.3 and 6.4

4 SE15: Arrays15–4 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]);

5 SE15: Arrays15–5 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); }

6 SE15: Arrays15–6 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; }

7 SE15: Arrays15–7 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

8 SE15: Arrays15–8 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

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

10 SE15: Arrays15–10 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]; }

11 SE15: Arrays15–11 Summary Looked at example programs that Illustrate the pitfalls Show how to pass and return Arrays in methods Demonstrate how to use arrays as instance variables How to sort arrays using selection sort

12 SE15: Arrays15–12 Follow-up Work Savitch chapter 6 Re-write the sort program to sort an Array of Strings into Alphabetical order.


Download ppt "Lecture 16 Arrays: Part 2 COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google