Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array...

40
Multidimensional Arrays

Transcript of Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array...

Page 1: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Multidimensional Arrays

Page 2: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Assignment 5 Demo

Page 3: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Announcements

● Assignment 4 due right now.

● Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM.● Play around with arrays, sound processing, and

image processing!● Send secret messages to your friends!● Compose music!● Fix broken family photos!

● YEAH hours (assignment review hours) next Monday from 7PM – 9PM in Herrin T175.

Page 4: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Arrays

137 42 314 271 160 178

0 1 2 3 4 5

● An array stores a sequence of multiple objects.● Can access objects by index using [].

● All stored objects have the same type.● You get to choose the type!

● Can store any type, even primitive types.● Size is fixed; cannot grow once created.

Page 5: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Basic Array Operations

● To create a new array, specify the type of the array and the size in the call to new:

Type[] arr = new Type[size]● To access an element of the array, use the

square brackets to choose the index:

arr[index]

● To read the length of an array, you can read the length field:

arr.length

Page 6: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,
Page 7: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,
Page 8: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Multidimensional Arrays

● You can create multidimensional arrays to represent multidimensional data.

int[][] a = new int[3][5];

a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]

a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]

a[2][0] a[2][1] a[2][2] a[2][3] a[2][4]

a[1]

a[2]

a[0]

Page 9: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Multidimensional Arrays

● You can create multidimensional arrays to represent multidimensional data.

Type[][] a = new Type[rows][cols];

a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]

a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]

a[2][0] a[2][1] a[2][2] a[2][3] a[2][4]

a[1]

a[2]

a[0]

Page 10: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Interpreting Multidimensional Arrays

● There are two main ways of intuiting a multidimensional array.

● As a 2D Grid:● Looking up arr[row][col] selects the element in

the array at position (row, col).

● As an array of arrays:● Looking up arr[row] gives back a

one-dimensional consisting of the columns in row row.

Page 11: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Loops and Multidimensional Arrays

● The canonical way to loop over a multidimensional array is with a double for loop:

Type[][] arr = /* … */

for (int row = 0; row < arr.length; row++) {

for (int col = 0; col < arr[row].length; col++) {

/* … access arr[row][col] … */

}

}

Page 12: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

00000

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 13: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

0 00000

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

0000

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 14: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

0 00 0000

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

000

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 15: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

0 00 0001

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

000

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 16: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

1 00 10 000

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

00

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 17: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

1 00 10 002

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

00

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 18: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

2 01 20 10 00

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

0

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 19: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

2 01 20 10 03

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

0

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 20: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

3 02 31 20 10 0

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 21: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

3 02 31 20 10 4

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

00000

00000

00000

0

1

2

3

0 1 21 3 4

Page 22: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

4

0

3 42 31 20 10

00000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

0000

00000

00000

0

1

2

3

0 1 21 3 4

Page 23: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

4

0

3 42 31 20 10

00001

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

0000

00000

00000

0

1

2

3

0 1 21 3 4

Page 24: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

1 0

4

1

3 42 31 20 10

0000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

000

00000

00000

0

1

2

3

0 1 21 3 4

Page 25: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

1 0

4

1

3 42 31 20 10

0002

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

000

00000

00000

0

1

2

3

0 1 21 3 4

Page 26: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

2 01 2

4

1

3 42 31 20 10

000

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

00

00000

00000

0

1

2

3

0 1 21 3 4

Page 27: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

2 01 2

4

1

3 42 31 20 10

003

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

00

00000

00000

0

1

2

3

0 1 21 3 4

Page 28: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

3 02 31 2

4

1

3 42 31 20 10

00

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

0

00000

00000

0

1

2

3

0 1 21 3 4

Page 29: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

3 02 31 2

4

1

3 42 31 20 10

04

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

0

00000

00000

0

1

2

3

0 1 21 3 4

Page 30: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

4 03 42 31 2

4

1

3 42 31 20 10

0

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

00000

00000

0

1

2

3

0 1 21 3 4

Page 31: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

4 03 42 31 2

4

1

3 42 31 20 10

5

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

00000

00000

0

1

2

3

0 1 21 3 4

Page 32: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

0

54 53 42 31 2

4

1

3 42 31 20 10

00000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

0000

00000

0

1

2

3

0 1 21 3 4

Page 33: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

02

54 53 42 31 2

4

1

3 42 31 20 10

0000

00000

int[][] arr = new int[4][5];for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = row + col; }}

Loops and Multidimensional Arrays

6543

76543

0

1

2

3

0 1 21 3 4

Page 34: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Working with Images

http://www.videogameobsession.com/videogame/wallpapers/VGO-NES-SuperMario_03-vgo.png

Page 35: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Representations of Color

● The human eye has three different types of color receptors that pick up colors (close to) red, green, and blue.

● Computers usually represent color as RGB triplets:● Describe the intensity of the red, green, and

blue components of the color.● Values typically range from 0 to 255,

inclusive.

Page 36: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,
Page 37: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Early Color Photographs

http://en.m.wikipedia.org/wiki/File:Rgb-compose-Alim_Khan.jpg

This picture was taken in 1911 by -Серге́й Миха́йлович Проку́дин Го́рский

(Sergei Mikhailovich Prokudin-Gorskii)

This picture was taken in 1911 by -Серге́й Миха́йлович Проку́дин Го́рский

(Sergei Mikhailovich Prokudin-Gorskii)

Page 38: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Creating GImages

● It is possible to directly create a GImage by specifying the RGB values of each pixel in the image.

● To do so:● Create an int[][] two-dimensional array to

hold the pixel values.● Use GImage.createRGBPixel to convert the

RGB triplets to int.● Construct a new GImage from the array.

Page 39: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

Manipulating Images

● You can extract an array of pixels from a GImage by calling

image.getPixelArray() ● You can then create a new image by

changing the pixel values.

Page 40: Multidimensional Arrays · Announcements Assignment 4 due right now. Assignment 5 (Array Algorithms) out today, due Monday, March 4 at 3:15PM. Play around with arrays, sound processing,

A Note: static Methods

● The methods

GImage.createRGBPixel

GImage.getRed

GImage.getGreen

GImage.getBlue

are called static methods.

● Static methods are invoked on a class rather than an object. They cannot access instance variables.

● Useful when the method represents a general operation that does not change an object.