lecture 18/19 arrays comp1681 / se15 introduction to programming

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

Upload: christal-robbins

Post on 03-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Lecture 18/19

Arrays

COMP1681 / SE15

Introductionto Programming

Page 2: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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

Page 3: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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

Page 4: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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?

Page 5: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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!

Page 6: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–6

Accessing Arrays

You can use these in similar ways to other variablestemperature[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]);

Page 7: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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]

32 24 26 30 31.5 28 29

0 1 2 3 4 5 6 Indices

temperature[4]

Page 8: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–8

More Details

The base type can be any typeBook myBook = new Book[10];

The integer in the square brackets can be an expression that evaluates to an integerSystem.out.println(“ How many temperatures will be entered? ”);int size = keyboard.nextInt();double[] temperature = new double[size];ORint day = 1;

System.out.println(“temp 3 days later” + temperature[day+3]);

Page 9: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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

Page 10: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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.

Page 11: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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

Page 12: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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]);

Page 13: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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); }

Page 14: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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;

}

Page 15: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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

Page 16: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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 indexth smallest element in a[index]

Savitch 6.4

Page 17: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–17

Selection Sort

32 27 24 26 30 31 28 29

a[0] a[1] a[2] a[3] a[4] a[5] a[6] Unsorted

24 27 32 26 30 31 28 29

24 26 32 27 30 31 28 29

24 26 27 32 30 31 28 29

24 26 27 28 29 30 31 32

.

.

Page 18: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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];

}

Page 19: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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

Page 20: Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays 18–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.