1 / 13

Multi-Dimensional Arrays

Multi-Dimensional Arrays. Rectangular & Jagged Plus: 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 1 st row

channer
Télécharger la présentation

Multi-Dimensional Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal

  2. ArrayRecall • 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);

  3. 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]]

  4. Jagged Arrays Multi-dimensional Arrays: Number of elements in a row can vary int[][] arr = new int[3][]; arr[0] = new int[2]; // 2 columns in row 0 arr[1] = new int[4]; // 4 columns in row 1 arr[2] = new int[3]; // 3 columns in row 2 To print this array: for(inti = 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(); }

  5. Jagged Array Example • Create the following array, and the print it: • Output (with tabs between values in a row): 1 • 2 1 2 3 jag 1 1 2 1 2 3

  6. 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

  7. 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

  8. An Array of Tallies // assume n = 669260267 int[] 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

  9. Tally Solution // Returns the digit value that occurs most frequently in n. // Breaks ties by choosing the smaller value. public static intmostFrequentDigit(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 intbestIndex = 0; for (inti = 1; i < counts.length; i++) { if (counts[i] > counts[bestIndex]) { bestIndex = i; } } return bestIndex; } based on notes from Reges and Stepp, Building Java Programs

  10. Array Histogram Question • Given a file of integer exam scores, such as: 82 66 79 63 83 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

  11. 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 (inti = 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

  12. Exercises What 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)); } }

  13. Exercise Output? int[][] x = {{1, 2, 3}, {1, 2}, {1}}; int sum = 0; for(inti = 0; i < x.length; i++) { sum += x[i].length; } System.out.println(sum);

More Related