Chapter 11—Arrays and ArrayLists
description
Transcript of Chapter 11—Arrays and ArrayLists
![Page 1: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/1.jpg)
Chapter 11—Arrays and ArrayLists
The Art and Science of
An Introductionto Computer ScienceERIC S. ROBERTS
Java
Arrays and ArrayListsC H A P T E R 1 1
Little boxes, on a hillside, little boxes made of ticky-tackyLittle boxes, little boxes, little boxes, all the sameThere’s a green one and a pink one and a blue one and a yellow oneAnd they're all made out of ticky-tacky and they all look just the same
—Malvina Reynolds, “Little Boxes,” 1962
CS101 @ Özyeğin University
Slides are adapted from the originals available athttp://www-cs-faculty.stanford.edu/~eroberts/books/ArtAndScienceOfJava/
Lecture Slides, Part IV
1
![Page 2: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/2.jpg)
Multidimensional Arrays• Because the elements of an array can be of any Java type,
those elements can themselves be arrays. Arrays of arrays are called multidimensional arrays.
• In Java, you can create a multidimensional array by using multiple brackets in both the type and the initialization parts of the declaration. For example, you can create array space for a 3 x 3 tic-tac-toe board using the following declaration:
char[][] board = new char[3][3];
• This declaration creates a two-dimensional array of characters that is organized like this:
board[0][0] board[0][1] board[0][2]
board[1][0] board[1][1] board[1][2]
board[2][0] board[2][1] board[2][2]
![Page 3: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/3.jpg)
Memory Layoutchar[][] board = new char[3][3];
0
1
2
board
0 1
0 0 02
0 1
0 0 02
0 1
0 0 02
![Page 4: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/4.jpg)
Memory Layoutchar[][] board = new char[3][3];board[0][0] = ‘X’;
0
1
2
board
0 1
‘X’ 0 02
0 1
0 0 02
0 1
0 0 02
![Page 5: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/5.jpg)
Memory Layoutchar[][] board = new char[3][3];board[0][0] = ‘X’;board[1][0] = ‘O’;
0
1
2
board
0 1
‘X’ 0 02
0 1
‘O’ 0 02
0 1
0 0 02
![Page 6: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/6.jpg)
Memory Layoutchar[][] board = new char[3][3];board[0][0] = ‘X’;board[1][0] = ‘O’;board[2][1] = ‘X’;
0
1
2
board
0 1
‘X’ 0 02
0 1
‘O’ 0 02
0 1
0 ‘X’ 02
![Page 7: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/7.jpg)
Exercise 1int[][] matrix = new int[3][4];
0
1
2
matrix
0 1
0 0 02
0 1
0 0 02
0 1
0 0 02
03
03
03
0
1
2
matrix
0 1
0 0 02
0 1
0 0 02
0 1
0 0 02
3
0 1
0 0 02
(A) (B)
![Page 8: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/8.jpg)
Exercise 2int[][] matrix = new int[3][4];matrix[3][4] = 42;println(matrix[0][0] + matrix[3][4]);
(B) 42(A) 0
(C) Error
![Page 9: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/9.jpg)
Exercise 3int[][] matrix = new int[3][4];println(matrix.length);
(B) 3(A) 4
(C) 12
(D) 7
![Page 10: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/10.jpg)
Exercise 4int[][] matrix = new int[3][4];println(matrix[0].length);
(B) 3(A) 4
(C) 12
(D) 7
![Page 11: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/11.jpg)
Exercise 5int[][] matrix = new int[3][4];println(matrix[2].length);
(B) 3(A) 4
(C) 12
(D) 7
![Page 12: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/12.jpg)
Traversing a two-dimensional arrayint[][] matrix = new int[3][4];
for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix[0].length; j++) { matrix[i][j] = i * j; }}
0
1
2
matrix
0 1
0 0 02
0 1
0 1 22
0 1
0 2 42
03
33
63
![Page 13: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/13.jpg)
Exercise 6int[][] matrix = new int[3][4];
for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix[0].length; j++) { matrix[i][j] = ???; }}
0
1
2
matrix
0 1
0 1 22
0 1
1 2 32
0 1
2 3 42
33
43
53
(B)i * j
(A)i + j
(C)matrix.length + j
(D)matrix[0].length + i
![Page 14: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/14.jpg)
Exercise 7int[][] matrix = new int[3][4];
for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix[0].length; j++) { matrix[i][j] = ???; }}
0
1
2
matrix
0 1
0 1 22
0 1
4 5 62
0 1
8 9 102
33
73
113
(B)matrix.length*i + j
(A)i + j
(C)matrix[0].length*i+j
(D)matrix.length*j + i
![Page 15: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/15.jpg)
Exercise 8int[][] matrix = new int[3][4];
for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix[0].length; j++) { matrix[i][j] = ???; }}
0
1
2
matrix
0 1
0 3 62
0 1
1 4 72
0 1
2 5 82
93
103
113
(B)matrix.length*i + j
(A)matrix[0].length*j+i
(C)matrix[0].length*i+j
(D)matrix.length*j + i
![Page 16: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/16.jpg)
Exercise 9int[][] matrix = new int[3][3];
for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix[0].length; j++) { if(i >= j) { matrix[i][j] = 1; } }}1 1 1
0 1 1
0 0 1
1 0 0
1 1 0
1 1 1
0 1 1
0 0 1
0 0 0
0 0 0
1 0 0
1 1 0
(D)
(B)
(C)
(A)
![Page 17: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/17.jpg)
Image Processing• Images consist of a set of pixels
arranged in a two-dimensional array, as shown in the expanded image.
• In the ACM library, images are represented using the GImage class.
GImage logo = new GImage("JTFLogo.gif");add(logo);
![Page 18: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/18.jpg)
Pixel Values• Each individual element in a pixel array is an int in which
the 32 bits are interpreted as follows:
• The first byte of the pixel value specifies the transparency of the color.
1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 0 0 1 1 0 0 1 1
• The next three bytes indicate the amount of red, green, and blue in the pixel, in which each value varies from 0 to 255. Together, these three bytes form the RGB value of the color, which is typically expressed using six hexadecimal digits. The color in the example has the RGB value 0x996633, which is a light brown:
transparency (α) red green blue
![Page 19: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/19.jpg)
Transparency• The first byte of the pixel value specifies the transparency of
the color, which indicates how much of the background shows through. This value is often denoted using the Greek letter alpha (α).
• Transparency values vary from 0 to 255. The value 0 is used to indicate a completely transparent color in which only the background appears. The value 255 indicates an opaque color that completely obscures the background. The standard color constants all have alpha values of 255.
• Fully transparent colors are particularly useful in images, because they make it possible to display images that do not have rectangular outlines. For example, if the gray pixels in the corners of the JTFLogo.gif image have an alpha value of 0, the background will show through those parts of the logo.
![Page 20: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/20.jpg)
Image Manipulation
pixel at (i,j) pixel at (height-i-1,j)
![Page 21: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/21.jpg)
public int[][] mirrorVertically(int[][] pixels) { int w = pixels[0].length; int h = pixels.length; int[][] result = new int[h][w]; for(int i = 0; i < result.length; i++) { for(int j = 0; j < result[0].length; j++) { result[h - i - 1][j] = pixels[i][j]; } } return result;}
![Page 22: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/22.jpg)
public void run() { // Images must be placed under the following folder: // workspace -> CS101 -> bin // assuming that your project is called "CS101" GImage yoda = new GImage("yoda.jpg"); // centralize the location of the image yoda.setLocation((getWidth() - yoda.getWidth())/2, (getHeight() - yoda.getHeight())/2); add(yoda); pause(1000); int[][] pixels = getImagePixels(yoda); int[][] newPixels = mirrorVertically(pixels); setImagePixels(yoda, newPixels);}
![Page 23: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/23.jpg)
/********************************************************* * Methods given below are helper methods. * Do not try to understand their details. *********************************************************/
/* This method sets the pixels of the given GImage object * to the given pixel array. * Ignore the implementation details. */public void setImagePixels(GImage img, int[][] newPixels) { int w = newPixels[0].length; int h = newPixels.length; int[] pixels = new int[w*h]; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { pixels[i * w + j] = newPixels[i][j]; } }
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); image.setRGB(0, 0, w, h, pixels, 0, w); img.setImage(image); img.setBounds((getWidth() - w)/2, (getHeight() - h)/2, w, h);}
/* This method reads the pixels of the given GImage * and returns them as a two-dimensional array. * Ignore the implementation details */public int[][] getImagePixels(GImage img) { int w = (int)img.getWidth(); int h = (int)img.getHeight(); int[] pixels = new int[w*h]; PixelGrabber pg = new PixelGrabber(img.getImage(), 0, 0, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { println("Problem occurred. Contact the course staff!"); } int[][] result = new int[h][w]; for(int i=0; i < h; i++) { for(int j=0; j < w; j++) { result[i][j] = pixels[i*w + j]; } } return result;}
public void init() { setSize(600, 450);}
![Page 24: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/24.jpg)
Rotate 180 Degrees
pixel at (i,j) pixel at (height-i-1, width-j-1)
![Page 25: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/25.jpg)
public int[][] rotate180Degrees(int[][] pixels) { int w = pixels[0].length; int h = pixels.length; int[][] result = new int[h][w]; for(int i = 0; i < result.length; i++) { for(int j = 0; j < result[0].length; j++) { result[h - i -1][w - j - 1] = pixels[i][j]; } } return result;}
![Page 26: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/26.jpg)
Mirror Horizontally (self-exercise)
pixel at (i,j) pixel at (?,?)
![Page 27: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/27.jpg)
Rotate Clockwise (self-exercise)
pixel at (i,j) pixel at (?,?)
What’s the width and height of the new image?
![Page 28: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/28.jpg)
Mirror Diagonally (self-exercise)
![Page 29: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/29.jpg)
Half-Mirror Horizontally (self-exercise)
![Page 30: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/30.jpg)
Green Screen Replacement
F B R
if F(i,j) is not green R(i,j) = F(i,j)else R(i,j) = B(i,j)
A color (r,g,b) is consideredgreen when r < 50, b < 50,and g > 100.
![Page 31: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/31.jpg)
public int[][] greenScreenEffect(int[][] foreground, int[][] background) { ???}
![Page 32: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/32.jpg)
public void run() { GImage superman = new GImage("superman.jpg"); GImage istanbul = new GImage("istanbul.jpg"); // centralize the location of the image superman.setLocation((getWidth() - superman.getWidth())/2, (getHeight() - superman.getHeight())/2); add(superman); pause(1000); int[][] supermanPixels = getImagePixels(superman); int[][] istanbulPixels = getImagePixels(istanbul); int[][] newSupermanPixels = greenScreenEffect(supermanPixels, istanbulPixels); setImagePixels(superman, newSupermanPixels);}
![Page 33: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/33.jpg)
/********************************************************* * Methods given below are helper methods. * Do not try to understand their details. *********************************************************/ /* Returns the red color component of a given pixel value * Ignore the implementation details. */ public int getRedComponent(int pixel) { return (pixel >> 16) & 0xff; }
/* Returns the green color component of a given pixel value * Ignore the implementation details. */ public int getGreenComponent(int pixel) { return (pixel >> 8) & 0xff; }
/* Returns the blue color component of a given pixel value * Ignore the implementation details. */ public int getBlueComponent(int pixel) { return pixel & 0xff; }
![Page 34: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/34.jpg)
Black and Whiten (self-exercise)
Use the getRed/Green/BlueComponent methods to decide if a pixel is dark or light.Use pixel value 0xFFFFFF for white, 0 for black.
![Page 35: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/35.jpg)
Lights Out• http://en.wikipedia.org/wiki/Lights_Out_%28game%29
![Page 36: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/36.jpg)
![Page 37: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/37.jpg)
public void toggleLight(int i, int j) { if(i >= 0 && i < lights.length && j >= 0 && j < lights[0].length) { boolean isOff = lights[i][j].isFilled(); lights[i][j].setFilled(!isOff); }}
public void pressOnLight(int i, int j) { toggleLight(i,j); toggleLight(i-1, j); toggleLight(i+1, j); toggleLight(i, j-1); toggleLight(i, j+1); if(isPuzzleSolved()) { GLabel lbl = new GLabel("SOLVED!", 0, 0); lbl.setFont("Courier-36"); lbl.setColor(Color.WHITE); lbl.setLocation((getWidth()-lbl.getWidth())/2, getHeight()/2); add(lbl); }}
![Page 38: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/38.jpg)
public boolean isPuzzleSolved() { for(int i=0; i < lights.length; i++) { for(int j=0; j < lights[0].length; j++) { if(!lights[i][j].isFilled()) { // light is lit return false; } } } return true;}
![Page 39: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/39.jpg)
// This method is invoked whenever the user presses the mouse button// The method finds out which light is pressed on// and invokes the pressOnLight() methodpublic void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); int i = y / LIGHT_SIZE; int j = x / LIGHT_SIZE; pressOnLight(i, j);}
public void init() { setSize(DIMENSION * LIGHT_SIZE, DIMENSION * LIGHT_SIZE); addMouseListeners();}
![Page 40: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/40.jpg)
Heat Transfer• A space can be considered as a collection of cells.• Each cell has a temperature. • Each cell transfers heat from/to its neighbors.
• Compute this equation for each cell, forever.
Tnew = Told + k(TN – Told)ΣN is a neighbor
High k: quick transferLow k: slow transfer
![Page 41: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/41.jpg)
![Page 42: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/42.jpg)
Initializing Multidimensional Arrays• You can initialize a multidimensional array when you declare
it by using nested braces to reflect the levels of array nesting.• For example, you can declare and initialize a multiplication
table for the digits 0 to 9 like this:
private static int[][] MULTIPLICATION_TABLE = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 }, { 0, 3, 6, 9, 12, 15, 18, 21, 24, 27 }, { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36 }, { 0, 5, 10, 15, 20, 25, 30, 35, 40, 45 }, { 0, 6, 12, 18, 24, 30, 36, 42, 48, 56 }, { 0, 7, 14, 21, 28, 35, 42, 49, 56, 63 }, { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72 }, { 0, 9, 18, 27, 36, 45, 54, 63, 72, 81 } };
![Page 43: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/43.jpg)
Exercise: Multidimensional ArraysWrite a single Java statement that declares and initializes a two-dimensional array named chessboard so that the array contains a representation of the initial position in a game of chess:
The individual elements should be characters using the standard symbols for the pieces:
![Page 44: Chapter 11—Arrays and ArrayLists](https://reader036.fdocuments.in/reader036/viewer/2022062310/56815db7550346895dcbe654/html5/thumbnails/44.jpg)
Solution: Chessboard Problem
private char[][] chessboard = { { 'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r' }, { 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, { 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' }, { 'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R' }, };