Upload
kerrie-melton
View
216
Download
0
Embed Size (px)
Citation preview
Multi-Dimensional Arrays
Rectangular & JaggedPlus: More 1D traversal
Array Recall• Creating a 2D array of doubles:
• double[][] grid = new double[3][4]; • 3 rows: indices 0 to 2, 4 columns: indices 0 to 3• grid: reference to entire array• grid[0]: reference to 1st row• grid[0][2]: third element in first row• all entries initialized to 0.0
• 1D array of Point objects: • Point[] points = new Point[3];• points[0] = new Point(2, 5); • points[1] = new Point(0, 0);• points[2] = new Point(4, 7);
Printing 2D Arrays• Arrays.toString() does not work well for 2D• Produces a String representation of each
element, but for a 2D array, each element is an array.
• Use Arrays.deepToString() instead:double[][] grid = new double[3][4];grid[0][1] = 5;grid[2][3] = 7;System.out.println(Arrays.deepToString(grid));
Output:[[0.0, 5.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 7.0]]
Jagged Arrays
Multi-dimensional Arrays:Number of elements in a row can varyint[][] arr = new int[3][]; arr[0] = new int[2]; // 2 columns in row 0arr[1] = new int[4]; // 4 columns in row 1arr[2] = new int[3]; // 3 columns in row 2
To print this array: for(int i = 0; i < arr.length; i++) { // print row i for(int j = 0; j < arr[i].length; j++) System.out.print(arr[i][j] + “\t”); System.out.println();}
Jagged Array Example• Create the following array, and the print it:
• Output (with tabs between values in a row):11 2 1 2 3
jag 11 21 2 3
Tallying Values with Arrays• Problem: Write a method mostFrequentDigit that returns the digit value that occurs most frequently in a number.
– Example: The number 669260267 contains: one 0, two 2s, four 6es, one 7, and one
9.mostFrequentDigit(669260267) returns 6.
– If there is a tie, return the digit with the lower value.mostFrequentDigit(57135203) returns 3.
based on notes from Reges and Stepp, Building Java Programs
Multi-Counter Problem• We could declare 10 counter variables ...
int counter0, counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8, counter9;
• But a better solution is to use an array of size 10.– The element at index i will store the counter for digit value i.– Example for 669260267:
– How do we build such an array? And how does it help?based on notes from Reges and Stepp, Building Java Programs
index
0 1 2 3 4 5 6 7 8 9
value
1 0 2 0 0 0 4 1 0 0
An Array of Tallies// assume n = 669260267int[] counts = new int[10];while (n > 0) { // pluck off a digit and add to proper counter int digit = n % 10; counts[digit]++; n = n / 10;}
based on notes from Reges and Stepp, Building Java Programs
index
0 1 2 3 4 5 6 7 8 9
value
1 0 2 0 0 0 4 1 0 0
Tally Solution// Returns the digit value that occurs most frequently in n.// Breaks ties by choosing the smaller value.public static int mostFrequentDigit(int n) { int[] counts = new int[10]; while (n > 0) { int digit = n % 10; // pluck off a digit and tally it counts[digit]++; n = n / 10; } // find the most frequently occurring digit int bestIndex = 0; for (int i = 1; i < counts.length; i++) { if (counts[i] > counts[bestIndex]) { bestIndex = i; } } return bestIndex;}
based on notes from Reges and Stepp, Building Java Programs
Array Histogram Question
• Given a file of integer exam scores, such as:8266796383
Write a program that will print a histogram of stars indicating the number of students who earned each unique exam score.
85: *****86: ************87: ***88: *91: ****
based on notes from Reges and Stepp, Building JavaPrograms
Array Histogram// Reads a file of test scores and shows a histogram of score distribution.import java.io.*;import java.util.*;
public class Histogram { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("midterm.txt")); int[] counts = new int[101]; // counters of test scores 0 - 100
while (input.hasNextInt()) { // read file into counts array int score = input.nextInt(); counts[score]++; // if score is 87, then counts[87]++ }
for (int i = 0; i < counts.length; i++) { // print star histogram if (counts[i] > 0) { System.out.print(i + ": "); for (int j = 0; j < counts[i]; j++) { System.out.print("*"); } System.out.println(); } } }}
based on notes from Reges and Stepp, Building Java Programs
ExercisesWhat is the output?public class Mystery { public static void main(String[] args) { int x = 1; int[] a = new int[2]; mystery2(x, a); System.out.println(x + “ “ + Arrays.toString(a)); x--; a[1] = a.length; mystery2(x, a); System.out.println(x + “ “ + Arrays.toString(a)); }
public static void mystery2(int x, int[] list) { list[x]++; x++; System.out.println(x + “ “ + Arrays.toString(list)); }}
ExerciseOutput?int[][] x = {{1, 2, 3}, {1, 2}, {1}};int sum = 0;for(int i = 0; i < x.length; i++) { sum += x[i].length;}System.out.println(sum);