Docsity

Arrays and Array Processing in Java: From One-Dimensional to Two-Dimensional, Slides of Computer Programming

Partial preview of the text

Download Arrays and Array Processing in Java: From One-Dimensional to Two-Dimensional and more Slides Computer Programming in PDF only on Docsity!

1.4 Arrays

!

2

A Foundation for Programming

objects functions and modules graphics, sound, and image I/O arrays any program you might want to write conditionals and loops Math text I/O primitive data types assignment statements store and manipulate huge quantities of data

4

Many Variables of the Same Type

Goal. 10 variables of the same type.

// tedious and error-prone double a0, a1, a2, a3, a4, a5, a6, a7, a8, a9; a0 = 0.0; a1 = 0.0; a2 = 0.0; a3 = 0.0; a4 = 0.0; a5 = 0.0; a6 = 0.0; a7 = 0.0; a8 = 0.0; a9 = 0.0; … a4 = 3.0; … a4 = 8.0; … double x = a4 + a8;

5

Many Variables of the Same Type

Goal. 10 variables of the same type.

// easy alternative double[] a = new double[ 10 ]; … a[ 4 ] = 3.0; … a[ 8 ] = 8.0; … double x = a[ 4 ] + a[ 8 ]; declares, creates, and initializes [stay tuned for details]

8

Arrays in Java

Java has special language support for arrays.

! To make an array: declare, create, and initialize it.

! To access element i of array named a, use a[i].

! Array indices start at 0.

Compact alternative.

! Declare, create, and initialize in one statement.

! Default initialization: all numbers automatically set to zero.

int N = 10 ; // size of array double[] a = new double[N]; // declare, create, init int N = 10 ; // size of array double[] a; // declare the array a = new double[N]; // create the array for (int i = 0 ; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.

9

Vector Dot Product

Dot product. Given two vectors x[] and y[] of length N, their dot

product is the sum of the products of their corresponding components.

double[] x = { 0.3, 0.6, 0.1 }; double[] y = { 0.5, 0.1, 0.4 }; int N = x.length; double sum = 0.0; for (int i = 0 ; i < N; i++) { sum = sum + x[i]*y[i]; }

Shuffling a Deck

12

Setting Array Values at Compile Time

Ex. Print a random card.

String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; int i = (int) (Math.random() * 13 ); // between 0 and 12 int j = (int) (Math.random() * 4 ); // between 0 and 3 System.out.println(rank[i] + " of " + suit[j]);

14

Shuffling

Goal. Given an array, rearrange its elements in random order.

Shuffling algorithm.

! In iteration i, pick random card from deck[i] through deck[N-1],

with each card equally likely.

! Exchange it with deck[i].

int N = deck.length; for (int i = 0 ; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); String t = deck[r]; deck[r] = deck[i]; deck[i] = t; } swap^ between i and N- idiom

15

Shuffling a Deck of Cards: Putting Everything Together

public class Deck { public static void main(String[] args) { String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; int SUITS = suit.length; int RANKS = rank.length; int N = SUITS * RANKS; String[] deck = new String[N]; for (int i = 0 ; i < RANKS; i++) for (int j = 0 ; j < SUITS; j++) deck[SUITS*i + j] = rank[i] + " of " + suit[j]; for (int i = 0 ; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); String t = deck[r]; deck[r] = deck[i]; deck[i] = t; } for (int i = 0 ; i < N; i++) System.out.println(deck[i]); } } avoid "hardwired" constants build the deck shuffle print shuffled deck

Coupon Collector

18

Coupon Collector Problem

Coupon collector problem. Given N different card types, how many

do you have to collect before you have (at least) one of each type?

Simulation algorithm. Repeatedly choose an integer i between 0 and N-1.

Stop when we have at least one card of every type.

Q. How to check if we've seen a card of type i?

A. Maintain a boolean array so that found[i] is true if we've already

collected a card of type i.

assuming each possibility is equally likely for each card that you collect

20

Coupon Collector: Debugging

Debugging. Add code to print contents of all variables.

Challenge. Debugging with arrays requires tracing many variables.

21

Coupon Collector: Mathematical Context

Coupon collector problem. Given N different possible cards, how many

do you have to collect before you have (at least) one of each type?

Fact. About N (1 + 1/2 + 1/3 + … + 1/N) ~ N ln N.

Ex. N = 30 baseball teams. Expect to wait " 120 years before

all teams win a World Series.

under idealized assumptions see ORF 245 or COS 340