arrays csc 171 fall 2002 lecture 20. arrays suppose we want to write a program that reads a set of...

91
Arrays Arrays CSC 171 FALL 2002 LECTURE 20

Post on 20-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ArraysArrays

CSC 171 FALL 2002

LECTURE 20

Page 2: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ArraysArrays

Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?65.2

81.7

31.3

95.4 < - highest grade

76.1

58.6

Page 3: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Individual data itemsIndividual data items

If we knew that there were always 150 students in the class, we could store the data as individual variables– score1,score2,score3, . . , score150

What would the data entry code look like?

What would the printing code look like?How about finding the max?

Page 4: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ArraysArrays

An array is a collection of data items of the same type

Every element of the collection can be accessed separately.

Page 5: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

___________________ is a fixed length sequence of values of the same type.

Page 6: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

_An array__________ is a fixed length sequence of values of the same type.

Page 7: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Constructing ArraysConstructing Arrays

double [] data = new double[10];

Page 8: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

double [] data = new double[10];

int x = data.length ;

//instance field on arrays, x = = 10

Page 9: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Setting Array valuesSetting Array valuesTo get values into an array you need to

specify which slot you want to use.Specification is done with the [ ] operatorThe [ ] operator follows the name of the

arrayThe [ ] operator encloses and integer-valued

expression called the index or subscript

Page 10: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Setting array valuesSetting array values

data[4] = 29.95;

0123456789

Page 11: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Using array valuesUsing array values

Similar to setting

int i = 4 ;

System.out.println(“data[“+i+”] ==“+ data[i]);

> data[4] ==29.95

Page 12: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

You access array elements with and integer position number, called the _______________, using the notation _______________.

Page 13: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

You access array elements with and integer position number, called the ___index_______, using the notation ____a[index]___________.

Page 14: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Array data itemsArray data itemsSuppose we want to write a program that

reads a set of test grades and prints them, marking the highest grade?

What would the data entry code look like?What would the printing code look like?How about finding the max?

Page 15: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Array issuesArray issues

Does it work?

double [] data = new double[10];

data[10] = 5.4;

Page 16: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Array issuesArray issues

Does it work?

double [] data = new double[10];data[10] = 5.4;

When the program runs, an out-of-bounds subscript generates an exception and terminates the program – why?

Page 17: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Position numbers of an array range from _________ to ____________.

Page 18: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Position numbers of an array range from ___0_______ to ____a.length -1_________.

Page 19: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Accessing a nonexistent position results is a ______________ error.

Page 20: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Accessing a nonexistent position results is a _bounds_____ error.

Page 21: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Use the ___________________field to find the number of elements in an array.

Page 22: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Use the _____length______field to find the number of elements in an array.

Page 23: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Array issuesArray issues

Can we search for the top grade as follows

double maxScore = data[0];

for (int i = 1;I<=data.length;i++)

If (data[i] > maxScore)

maxScore = data[i];

Page 24: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Array issuesArray issues

An array of length n has index values from 0 to (n-1)

double maxScore = data[0];

for (int i = 1;I<data.length;i++)

If (data[i] > maxScore)

maxScore = data[i];

Page 25: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Array issuesArray issues

Does it work?

public static void main(String[] args){ double[] data;

If (data[0] > 4.0)

System.out.println(“GT 4!”);

}

Page 26: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Array issuesArray issues

Arrays must be allocated!

public static void main(String[] args){ double[] data = new double[10];

If (data[0] > 4.0)

System.out.println(“GT 4!”);

}

Page 27: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Array issuesArray issues

Arrays can be initialized!

public static void main(String[] args){ double[] data = {2,3,4,5,6};

If (data[0] > 4.0)

System.out.println(“GT 4!”);

}

// note: new int[] {2,3,4,5,6} ; is also legal

Page 28: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Copying ArraysCopying Arrays

Is this ok?

double [] data = new double[10];

double[] testScores;

testScores = data;

Page 29: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Copying ArraysCopying Arrays

Is this ok?

double [] data = new double[10];

double[] testScores;

testScores = data;

How many arrays are there?

Page 30: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Copying Array REFERENCESCopying Array REFERENCESdouble [] data = new double[10];double[] testScores;testScores = data;

Page 31: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Copying ArraysCopying Arrays

So, what if we want to make two “real” copies – what does the code look like?

Write a method so that

int[] x = {3,4,5,6,7,8,9,10};

int[] y = myCopy(x);

Page 32: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

CopyingCopying

public static int[] myCopy(int[] x){

int[] r_arry = new int[x.length];

for (int i = 0 ; i< x.length;i++)

r_arry[i] = x[i];

return r_arry;

}

Page 33: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

System.arrayCopySystem.arrayCopy//System.arraycopy(from,fromstart,to,toStart,count);

System.arraycopy(data,0,prices,0,data.length);

Page 34: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

CloneClone

Page 35: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

An array variable stores a ________________________. Copying the variable yields a second ____________________ to _________________ array.

Page 36: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

An array variable stores a ______reference__________. Copying the variable yields a second ______reference______________ to ____the same_____________ array.

Page 37: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Use the ________________ method to copy the elements of an array.

Page 38: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Use the ____clone______ method to copy the elements of an array.

Page 39: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Use the _____________________________ method to copy elements from one array to another.

Page 40: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Use the _System.arraycopy____________________ method to copy elements from one array to another.

Page 41: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

If you run out of space in an array you need to _______________ a larger array and copy the elements into it.

Page 42: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

If you run out of space in an array you need to __allocate_____ a larger array and copy the elements into it.

Page 43: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Getting a Bigger ArrayGetting a Bigger Array

Page 44: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

More ArraysMore Arrays

Suppose we want to write a program that reads a set of test product names, prices, and quality scores prints them, marking the best value? (score/prices)Digital 500X, $3499.00, score 73

ZEOS Pentium-III/500, $2545.00, score 70

Micro Express MF, $2195.00, score 72 < - best value

Polywell Poly 450IP, $2099.00, score 30

Page 45: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Parallel ArraysParallel Arrays

One solution, can you think of a better one?

Page 46: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Arrays of ObjectsArrays of ObjectsEasier to deal with - arrays hold references

Page 47: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Avoid ___________________________ arrays by changing them into arrays of objects.

Page 48: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Avoid ____________parallel_______ arrays by changing them into arrays of objects.

Page 49: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Multidimensional ArraysMultidimensional Arrays

Arrays of arrays– Arrays are objects– Arrays hold references to objects– Ergo, arrays can hold arrays

Page 50: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Tables are 2D arraysTables are 2D arraysint size = 5;int[][] mtable = new int[size][size];for(i=0;i<size;i++)

for(int j=0;j<size;j++)mtable[i][j] = (i+1)*(j+1);

1 2 3 4 5

2 4 6 8 10

3 6 9 12 15

4 8 12 16 20

5 10 15 20 25

// how easy to modify code ????

int[][] mtable = new int[5][5];

for(i=0;i<5;i++)

for(int j=0;j<5;j++)

mtable[i][j] = (i+1)*(j+1);

Page 51: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Arrays of ArraysArrays of Arrays

int [][] powers = new int[10][10];

Page 52: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Arrays of ArraysArrays of Arrays

int [][] pow2 = {{1,2,3},{4,5,6},{7,8,9}};

1 2 3

4 5 6

7 8 9

Page 53: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Allocating arrays of arraysAllocating arrays of arrays

int size = 10;int[][] mtable = new int[size][];for(i=0;i<mtable.length;i++) {

mtable[i]= new int[size];for(int j=0;j<mtable[i].length;j++)

mtable[i][j] = (i+1)*(j+1);}

Page 54: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

AlternatelyAlternately

The following is legal.What is the structure?

int[][] b = new int[5][];

for (int i=0;i<b.length;i++)

b[i] = new int[i+1];

Page 55: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

AlternatelyAlternately

int[][] b = new int[5][];for (int i=0;i<b.length;i++)

b[i] = new int[i+1];

This is known as a triangular arrayIs b[3][1] a legal reference or b[1]

[3]?

Page 56: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Two dimensional arrays form a ___________________________ arrangement. You access elements with an index pair using the notation ______________.

Page 57: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Two dimensional arrays form a _tabular, two dimensional_______ arrangement. You access elements with an index pair using the notation _____a[i][j]_________.

Page 58: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Passing ArraysPassing ArraysIn JAVA

– Primitives are passed by value A copy of the variable is made & used Modifications made do not affect calling value public void myAdd(int x) { x++;}

– Objects are passed by reference Since the reference is passed it is possible to change

the value in the calling method Public void myAdd (int[] x) {x[0]++;}

Page 59: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Pass by valuePass by value

public class passArry {

public static void main(String args[]){

int x1 = 3;

System.out.println("x1 == "+x1);

myAdd(x1);

System.out.println("x1 == "+x1);

}

public static void myAdd(int x) {x++;}

}

Page 60: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Pass by referencePass by reference

public class passArry {

public static void main(String args[]){

int[] x2 = {4,5,6};

System.out.println("x2[0] == "+x2[0]);

myAdd(x2);

System.out.println("x2[0] == "+x2[0]);

}

public static void myAdd(int[] x) {x[0]++;}

}

Page 61: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Local referenceLocal reference

class Student {

int age;

// …..

public void setAge(int age){

age = age;

}

}

Page 62: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Local referenceLocal reference

class Student {

int age;

// …..

public void setAge(int age){

this.age = age;

}

}

Page 63: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ExerciseExercise

Write a method that takes an integer array and prints the values

Page 64: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ExerciseExercise

Write a method that takes an integer array and prints the values

public static int myPrint(int[] x){

for (int i = 0 ; i<x.length;i++)

System.out.println(x + “[“+i+”] == “+ x[i]);

}

Page 65: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ExerciseExercise

Write a method that takes an integer array, and two integer indices and swaps the value

Page 66: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ExerciseExerciseWrite a method that takes an integer array, and

two integer indices and swaps the value

public static void mySwap(int[] x, int i, int j){ int temp = x[i];

x[i] = x[j];

x[j] = temp;

}

Page 67: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Interpretive DanceInterpretive Dance

Some people are “visual” learners

Some people are “aural” learners

Some people are “symbolic” learners

Some people are “body” learners

Every kindergardener learns how to sort.

Page 68: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

SortingSorting

Write a method that takes an array and sorts via the kindergarden method.

Page 69: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

BubblesortBubblesortWrite a method that takes an array and

sorts via the kindergarden method.public static void bubbleSort(int[] x){

for (int j=0;j<x.length;j++)

for (int i = 0 ; i<x.length-1;i++)

if (x[i] > x[i+1]) mySwap(x,i,i+1);

}

// how many comparisions do we make?

Page 70: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Counting comparisonsCounting comparisons

If the length of the array is “n”

The outer loop executes “n” time

Each time the outer loop executes, the inner loop executes “(n-1)” time

So, we get n(n-1) comparisions

Page 71: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Exercise: SEARCHExercise: SEARCH

Write a method that takes an integer array and returns the index of the maximum value

Page 72: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ExerciseExerciseWrite a method that takes an

integer array and returns the index of the maximum value

public static int myMax(int[] x){ int rvalue = 0;for (int i = 0 ; i<x.length;i++) if (x[i] > x[rvalue]) rvalue = i;return rvalue;

}

Page 73: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ExerciseExercise

Overload the max finder to take an array and an index – the method now returns the index of the maximum value >= the index passed in

Page 74: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

ExerciseExerciseOverload the max finder to take an

array and an index – the method now returns the index of the maximum value >= the index passed in

public static int myMax(int[] x, int j){ int rvalue = j;for (int i = j ; i<x.length;i++) if (x[i] > x[rvalue]) rvalue = i;return rvalue;

}

Page 75: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

SortingSorting

Write a method that takes an integer arrayLoop through all the positions in the array,

one after the otherAt each (current) position, find the max

from that position to the rest of the array.Swap the value with the current positionThis is termed selection sort

Page 76: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Selection SortSelection SortDoes it work?public void selSort(int x){

for (int i = 0 ; i<x.length;i++){int temp = myMax(x,i);mySwap(x,i,temp);

}}Can you prove it works?What is the loop invariant?

Page 77: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Swap max “^” with first “|”Swap max “^” with first “|”

{1,9,2,8,3,7,4,6,5}

| ^ {9,1,2,8,3,7,4,6,5}

| ^ {9,8,2,1,3,7,4,6,5}

| ^ {9,8,7,1,3,2,4,6,5}

| ^

{9,8,7,6,3,2,4,1,5}

| ^ {9,8,7,6,5,2,4,1,3}

| ^ {9,8,7,6,5,4,2,1,3}

| ^ {9,8,7,6,5,4,3,1,2}

| ^

Page 78: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Run time analysisRun time analysis

How long does it take SectionSort to run on an array of a given length?

Page 79: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Count the loopsCount the loopsn is the length of the arrayThe outer (i) loop executes from 0 to n-

2 – n-1 times

The on each cycle of the outer loop i has a certain (different) value

On each cycle of the outer loop, the inner loop executes from I + 1 to n –1– n – i – 1 times

Page 80: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

So, we getSo, we get

N-2

n-i-1) i=0

Page 81: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Can we prove ?Can we prove ?

N-2

n-i-1) = = i=0

n(n-1)/2

Page 82: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Array ListsArray Lists

Consider Purse class Purse doesn't remember individual Coin objects,

just the total Don't know how many coins--can't have variables

coin1...coin10 Use ArrayList to store variable number of objects

ArrayList coins = new ArrayList();coins.add(new Coin(0.1, "dime"));. . .

size method yields number of elements

Page 83: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

An ______________________ is a sequence of objects.

Page 84: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

An _array list______ is a sequence of objects.

Page 85: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Retrieving Array List ElementsRetrieving Array List Elements

Use get method Index starts at 0 Must cast to correct type Coin c = coins.get(0); // gets first coin Bounds error if index is out of range Most common bounds error:

int n = coins.size();c = (Coin)coins.get(n); // ERROR // legal index values are 0...n-1

Page 86: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

To store a primitive type in an array list you must use a ____________ class.

Page 87: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

To store a primitive type in an array list you must use a __wrapper__ class.

Page 88: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

When retrieving an element from an array list, you need to ________________ the return value of the ________________ method to the element class.

Page 89: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

When retrieving an element from an array list, you need to __cast____ the return value of the _______get__ method to the element class.

Page 90: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Position number of an array list range from _________ to _______________. Accessing a nonexistent position results in a _________ error.

Page 91: Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?

Position number of an array list range from _____0____ to ________size() - 1_______. Accessing a nonexistent position results in a ___counds______ error.