Arrays - Orange Coast Collegefaculty.orangecoastcollege.edu/...Ch07_Arrays_P3.pdf · CS 170 _ Java...
-
Upload
nguyenthuy -
Category
Documents
-
view
216 -
download
1
Transcript of Arrays - Orange Coast Collegefaculty.orangecoastcollege.edu/...Ch07_Arrays_P3.pdf · CS 170 _ Java...
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 1
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Arrays
Chapter 7
Part 3 – Multi-Dimensional Arrays
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 2
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Agenda
• Array Dimensions
• Multi-dimensional arrays
▪ Basics
▪ Printing elements
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 3
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
First Some Video Tutorials• Matthew Guarnotta:
▪ 2D arrays in Java (9:36)
• CompSci Studio:
▪ Java Beginner Programming Tutorial 34 2D Arrays (6:43)
▪ Java Beginner Programming Tutorial 35 2D Array Grid (7:46)
• in28minutes:
▪ Java 2d array tutorial (9:37)
• MargretPosch
▪ Java Basics – 2D Arrays (14:39)
• 2D + Jagged arrays + Arrays class + toString to print elements
• Jose Vidal:
▪ Java Tic Tac Toe Board Program: 2D Array: Tutorial (8:38)
▪ Advanced topics:
• Uses toString() method, for each loops, constructors, & enum
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 4
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Array Dimensions
• The arrays we discussed so far are called
one-dimensional arrays
• Syntax review:
1. Declaring and then creating memory
dataType[ ] arrayName;
arrayName = new dataType[arraySize];
2. Declaring and creating memory in one stepdataType[ ] = new dataType[arraySize];
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 5
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Array Dimensions
• An array that has more than one index is
called multi-dimensional arrays
• An array that has two indexes is called two-
dimensional arrays (2D-Arrays or matrix)
• A 2D-array can be displayed as a two-
dimensional table
• 2D arrays are commonly used in financial and
scientific applications
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 6
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
2D Array Declaration
• Syntax :
1. Declaring and then creating memory
dataType[][] arrayName;
arrayName =
new dataType[rowSize][columnSize];
2. Declaring and creating memory in one step
dataType[][] arrayName = new
dataType[rowSize][columnSize];
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 7
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
2D Arrays Example
• Assume you want to keep the medal count for
a specific Olympics
• How would you create a 2D array that
resembles this?
Gold Silver Bronze
Canada 1 1 0
China 1 0 1
Germany 1 2 0
Russia 2 1 1
USA 3 2 1
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 8
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
2D Arrays Example
• Array declarationint[][] medals = new int[5][3];
or
• Array initialization during declaration int[][] medals = { {1,1,0},
{1,0,1},
{1,2,0},
{2,1,1},
{3,2,1}
}
Gold Silver Bronze
Canada 1 1 0
China 1 0 1
Germany 1 2 0
Russia 2 1 1
USA 3 2 1
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 9
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
2D Arrays Example
• Accessing array elements
▪ How many silver medals did Germany get? System.out.printf(“Germany’s medals: %d”, medals[2][1]);
▪ Change the number of gold medals for Canada to be 2 medals [0][0] = 2;
Gold Silver Bronze
Canada 1 1 0
China 1 0 1
Germany 1 2 0
Russia 2 1 1
USA 3 2 1
0 1 2
0
1
2
3
4
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 10
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
2D Arrays Example
• To get the number of rows & columns, use the length instance variable
• To return the number of rowsSystem.out.print(medals.length);
• To return the number of columns
System.out.print(medals[anyRow].length);
Gold Silver Bronze
Canada 1 1 0
China 1 0 1
Germany 1 2 0
Russia 2 1 1
USA 3 2 1
0 1 2
0
1
2
3
4
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 11
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
2D Arrays
Example
Gold Silver Bronze
Canada 1 1 0
China 1 0 1
Germany 1 2 0
Russia 2 1 1
USA 3 2 1
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 12
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
2D Arrays Exercise
Gold Silver Bronze
Canada 1 1 0
China 1 0 1
Germany 1 2 0
Russia 2 1 1
USA 3 2 1
• Modify the code as follows:
▪ Add two, one-dimensional arrays to
save the countries and medal names
▪ Display the countries to the left and the
medal names at the top of the array
contents
• The output should look like the
figure
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 13
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
2D Arrays Exercise
• How will this array be populated?
int my2DArray [][] = new int [5][3];
for (int row = 0; row < my2DArray.length; ++row)
for (int col = 0; col < my2DArray[row].length; ++col)
my2DArray[row][col] = row + col;
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 14
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
2D Arrays Exercise
• How will this array be populated?
int my2DArray [][] = new int [5][3];
for (int row = 0; row < my2DArray.length; ++row)
for (int col = 0; col < my2DArray[row].length; ++col)
my2DArray[row][col] = row + col;
0 1 2
0 0 1 2
1 1 2 3
2 2 3 4
3 3 4 5
4 4 5 6
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 15
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Printing a 2D Arrays
for (int row = 0; row < my2DArray.length; ++row)
{
for (int col = 0; col < my2DArray[row].length; ++col)
System.out.print(my2DArray[row][col]+”\t“);
System.out.println();
}
0 1 2
0 0 1 2
1 1 2 3
2 2 3 4
3 3 4 5
4 4 5 6
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 16
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Printing a 2D Arrays• Exercise:
▪ What is printed if we switch the two for
statements?
▪ What must be changed in the code?
0 1 2
0 0 1 2
1 1 2 3
2 2 3 4
3 3 4 5
4 4 5 6
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 17
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Printing a 2D Arrays• Exercise:
▪ What is printed if we switch the two for
statements?
▪ What must be changed in the code?
0 1 2
0 0 1 2
1 1 2 3
2 2 3 4
3 3 4 5
4 4 5 6
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 18
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Printing a 2D Arrays• Exercise: What is printed if we switch the two for
statements?
for (int col = 0; col < my2DArray[1].length; ++col)
{
for (int row = 0; row < my2DArray.length; ++row)
System.out.print(my2DArray[row][col] + "\t");
System.out.println();
}
0 1 2
0 0 1 2
1 1 2 3
2 2 3 4
3 3 4 5
4 4 5 6
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 19
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Printing a 2D Arrays• Example: Add rows contents
0 1 2
0 0 1 2
1 1 2 3
2 2 3 4
3 3 4 5
4 4 5 6
for (int row = 0; row < my2DArray.length; ++row)
{
int sum = 0;
for (int col = 0; col < my2DArray[row].length; ++col)
sum += my2DArray[row][col];
System.out.println(“Row “ + row + “: “ + sum);
}
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 20
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
More Array Dimensions
• You can define three-dimensional arrays or n-
dimensional array (can be any number).
• Syntax:
dataType[]...[] arrayName = new dataType[idx1]...[idxn];
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 21
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
More Array Dimensions
• Example: ▪ An array of grades, where there are 40 students, all taking
the same 3 courses, and each course has 10 quizzes int [][][] grades = new int[4][3][2];
▪ Total # element = 4 x 3 x 2 = 24
▪ Read and fill the array of grades
▪ What could be a better names for the indices?
int [][][] grades = new int[4][3][2];
// Read from the user
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 3; ++j)
for (int k = 0; k < 2; ++k)
grades[i][j][k] = input.nextInt();
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 22
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
3-D Array Exercise• Create a program that will enter the values
and displays the contents of the 3-D array.
• Sample Output:
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 23
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review: Multidimensional-Array Basics
• Consider this table of values
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 24
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review: Multidimensional-Array Basics
• We specify the row and column indices for the array to access a specific element
table[3][2] has
a value of 1262
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 25
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review: Multidimensional-Array Basics
• We can access elements of the table with a nested for loop
• Example:
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 26
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Multidimensional-
Array Example
Sample
screen
output
CS 170 _ Java Programming - M. Malaty
Chapter 7: Arrays – Slide # 27
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Summary
• Multidimensional arrays are implemented as
an array of arrays
• Treat two-dimensional array as a table with
rows and columns