Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.

Similar presentations


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

1 Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming

2 SE15: Arrays15–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: Arrays15–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: Arrays15–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: Arrays15–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: Arrays15–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: Arrays15–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: Arrays15–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: Arrays15–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: Arrays15–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: Arrays15–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: Arrays15–12 Indexed Variables as Methods 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. newAverage = average(firstDay,temperature[i]);

13 SE15: Arrays15–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 void multiplyArray(double[] a, double factor) { int i; for i=0; i < a.length; a++) a[i] = a[i] * factor; }

14 SE15: Arrays15–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. double temp = new double[nextScore.length]; return temp;

15 SE15: Arrays15–15 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 Next time: Programming with Arrays and Classes

16 SE15: Arrays15–16 Follow-up Work Savitch chapter 6 Self Test Exercise 6 Self Test Exercise 9


Download ppt "Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google