36
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: [email protected] 06/17/22 1

Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: [email protected] 12/19/20151

Embed Size (px)

Citation preview

Page 1: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

Array - CIS 1068 Program Design and Abstraction

Zhen Jiang

CIS Dept.

Temple University

SERC 347, Main Campus

Email: [email protected]

04/21/23 1

Page 2: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

Table of Contents Problem Arrays Arrays and Loops A Multi-counter Problem Multi-dimensional Arrays Exercises Summary of Learning Materials

04/21/23 2

Page 3: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

3

How would you solve this?

Consider the following program:

How many days' temperatures? 7Day 1's high temp: 45Day 2's high temp: 44Day 3's high temp: 39Day 4's high temp: 48Day 5's high temp: 37Day 6's high temp: 46Day 7's high temp: 53Average temp = 44.571428571428574 days were above average.04/21/23

Page 4: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

4

We need each input value twice … to compute the average via a cumulative sum … to count how many were above the average

What about putting the values into variables?

How many variables would we declare?

Need a way to declare many variables at once (and also to control that number of variables declared).04/21/23

Page 5: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

5

Arrays array: An object that stores many values of the same type.

element: a value in an array index: an integer indicating the position of a value in an array

37284-617526-24912value

9876543210index

element 0 element 4 element 9

04/21/23

Page 6: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

6

Declaring/initializing an array:<type>[] <name> = new <type>[<length>];

Example:int[] numbers = new int[10];

The length can be any integer expression:int x = 2 * 3 + 1;int[] data = new int[x % 5 + 2];

0000000000value

9876543210index

04/21/23

Page 7: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

7

When arrays are initially constructed, every element is automatically initialized to a "zero-equivalent" value.

int: 0 double: 0.0 boolean: false object type: null (null means "no object")

04/21/23

Page 8: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

8

An array of doubles

An array of booleans

0.00.00.00.00.0value

43210index

falsefalsefalsefalsevalue

3210index

04/21/23

Page 9: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

9

Assigning a value to an array element:<array name>[<index>] = <value>;

Example:numbers[0] = 27;

numbers[3] = -6;

000000-60027value

9876543210index

04/21/23

Page 10: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

10

Using an array element's value in an expression:<array name>[<index>]

Example:System.out.println(numbers[0]);

if (numbers[3] < 0) {

System.out.println("Element 3 is negative.");

}

000000-60027value

9876543210index

04/21/23

Page 11: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

11

Reading or writing any index outside the valid range will throw an ArrayIndexOutOfBoundsException.

Example:int[] data = new int[10];System.out.println(data[0]); // okaySystem.out.println(data[-1]); // exception!System.out.println(data[9]); // okaySystem.out.println(data[10]); // exception!

0000000000value

9876543210index

04/21/23

Page 12: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

12

int[] numbers = new int[8];

numbers[1] = 4;

numbers[4] = 99;

numbers[7] = 2;

int x = numbers[1];

numbers[x] = 44;

numbers[numbers[7]] = 11; // use numbers[7] as index!

6 7543210

numbers: 0

6

0000000

7543210

0

6

20990040

7543210

0

6

204401140

7543210

x: 4x:

04/21/23

Page 13: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

13

Arrays and Loops

Arrays are very commonly used with for loops to access each element

This is a space problem: 1st element, 2nd element, … etc, compared with the time sequence problem we mentioned earlier (1st iteration, 2nd iteration, 3rd iteration, …)

Solution: 1st iteration handles 1st element, 2nd iteration handles 2nd element, …., etc

04/21/23

Page 14: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

14

Example:

for (int i = 0; i < 8; i++) {

System.out.print(numbers[i] + " ");

}

System.out.println(); // end the line of output

Output:0 4 11 0 44 0 0 2

6 7543210

numbers: 0

6

0000000

7543210

0

6

20990040

7543210

0

6

204401140

7543210

04/21/23

Page 15: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

15

Make an array:

for (int i = 0; i < 8; i++) {

numbers[i] = 2 * i;

}

14121086420value

76543210index

04/21/23

Page 16: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

16

Make an array:

for (int i = 0; i < 8; i++) {

numbers[i] = i * i;

}

493625169410value

76543210index

04/21/23

Page 17: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

17

An array's length field stores its number of elements.

General syntax:<array name>.length

NB: Because it's a field (i.e. not a method), it does not use parentheses like a String's .length()!

04/21/23

Page 18: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

18

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

System.out.print(numbers[i] + " ");

}

Output:0 1 4 9 16 25 36 49

What expression refers to the last element of an array? The middle element?

493625169410value

76543210index

04/21/23

Page 19: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

19

Solve the following problem:

How many days' temperatures? 7Day 1's high temp: 45Day 2's high temp: 44Day 3's high temp: 39Day 4's high temp: 48Day 5's high temp: 37Day 6's high temp: 46Day 7's high temp: 53Average temp = 44.571428571428574 days were above average.04/21/23

Page 20: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

20

import java.util.*;

public class Weather { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int days = console.nextInt(); int[] temperatures = new int[days]; // to store temperatures int sum = 0;

for (int i = 0; i < days; i++) { // process each day System.out.print("Day " + (i + 1) + "'s high temp: "); temperatures[i] = console.nextInt(); sum += temperatures[i]; } double average = (double) sum / days;

int count = 0; // see if above average for (int i = 0; i < days; i++) { if (temperatures[i] > average) { count++; } } // report results System.out.println("Average temp = " + average); System.out.println(count + " days above average"); }}

04/21/23

Page 21: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

21

A multi-counter problem

Problem: Examine a number and count the number of occurrences of every digit.

Example: The number 229231007 contains: two 0s, one 1, three 2s, one 7, and one 9

Solution? Declare 10 counter variables—one per digit. Eeewww!!!!

int counter0, counter1, counter2, counter3;

int counter4, counter5, counter6, counter7;

int counter8, counter9;

04/21/23

Page 22: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

22

Problem: Examine a number and count the number of occurrences of every digit.

Example: The number 229231007 contains: two 0s, one 1, three 2s, one 7, and one 9

Solution: Declare an array of 10 elements—the element at index i will store the counter for digit value i.

int[] counts = new int[10];

04/21/23

Page 23: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

23

int num = 229231007;

//ensure num > 0

int[] counts = new int[10];

while (num > 0) {

int digit = num % 10;

counts[digit]++;

num = num / 10;

}

1010000312value

9876543210index

04/21/23

Page 24: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

Write a program that will print a histogram of stars indicating the number of occurrences of every digit.

0: **1: *2: ***7: *9: *

for (int i = 0; i < counts.length; i++) { if (counts[i] > 0) { System.out.print(i + ": "); for (int j = 0; j < counts[i]; j++) { System.out.print("*"); } System.out.println(); }}

04/21/23 24

Page 25: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

25

Quick array initialization, general syntax:<type>[] <name> = {<value>, <value>, ..., <value>};

Example:int[] numbers = { 12, 49, -2, 26, 5, 17, -6 };

Useful when you know in advance what the array's element values will be.

-617526-24912value

6543210index

04/21/23

Page 26: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

26

int[] a = { 2, 5, 1, 6, 14, 7, 9 };

for (int i = 1; i < a.length; i++) {

a[i] += a[i - 1];

}

What’s in the array?

97146152value

6543210index

44352814872value

6543210index

04/21/23

Page 27: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

27

Arrays.toString accepts an array as a parameter and returns the String representation, which you can then print.

Example:int[] a = { 2, 5, 1, 6, 14, 7, 9 };for (int i = 1; i < a.length; i++) { a[i] += a[i - 1];}System.out.println("a is " + Arrays.toString(a));

Output:a is [2, 7, 8, 14, 28, 35, 44]

04/21/23

Page 28: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

28

Multi-dimensional ArraysLet’s say there’s a class of 8 students,

with 10 quiz grades each:

How can we declare variables to store all this data?

7.5 4.5 9.5 2 7 8.5 1 8.5 7.5 6

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

4.5 2 9 9 5.5 4 7.5 6 5 9

04/21/23

Page 29: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

29

double [] student1 = new double[10];

...

double [] student8 = new double[10];

student1:

...

student8:

Solution 1: An array per student. Tedious!

7.5 4.5 9.5 2 7 8.5 1 8.5 7.5 6

4.5 2 9 9 5.5 4 7.5 6 5 9

04/21/23

Page 30: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

30

double [] quiz1 = new double[8];

...

double [] quiz10 = new double[8];

quiz1:

...

quiz10:

Solution 2: An array per quiz. Same problem!

7.5 … … … … … … 4.5

6 … … … … … … 9

04/21/23

Page 31: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

31

double [][] quizScores = new double[8][10];

quizScores:

7.5 4.5 9.5 2 7 8.5 1 8.5 7.5 6

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

4.5 2 9 9 5.5 4 7.5 6 5 9

quizScores[0][6]

quizScores[7][9]

04/21/23

Page 32: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

A 2-D array is really an array of arrays!

double [][] quizScores = new double[4][3];quizScores

quizScores[0]

quizScores[1]

quizScores[2]

quizScores[3]

04/21/23 32

Page 33: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

double [][] quizScores = new double[4][];

quizScores[0] = new double[3];

quizScores[2] = new double[5];quizScores

quizScores[0]

quizScores[1]

quizScores[2]

quizScores[3]

04/21/23 33

Page 34: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

Exercises Compute the sum of the elements on the diagonal of a matrix.

Swap each element in a square 2-D array with elements on the opposite side of the main diagonal.

04/21/23 34

Page 35: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

Exercises, slides 14-16, 18, 20, 21, 23-24, 26-27, 34

Summary

04/21/23 35

Page 36: Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 12/19/20151

Arrays, element, index, p494 New, p494 Valid range, p503-4 Declaration and initialization, p494, p497 Length, p500 Arrays.toString, Multi-dimensional arrays, p545

04/21/23 36