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

Slides:



Advertisements
Similar presentations
1 Various Methods of Populating Arrays Randomly generated integers.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Building Java Programs Chapter 7 Arrays. 2 Can we solve this problem? Consider the following program (input underlined): How many days' temperatures?
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Topic 7 parameters Based on slides bu Marty Stepp and Stuart Reges from "We're flooding people with information. We.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 4.3, 7.6.
1 Reference semantics. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b.
CS 112 Introduction to Programming Reference Semantics; 2D Arrays; Array as State Yang (Richard) Yang Computer Science Department Yale University 308A.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.6, 4.3.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Building Java Programs Chapter 7
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.1, 4.4 self-checks: #1-9.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
BUILDING JAVA PROGRAMS CHAPTER 7: ARRAYS 1. 2 OUT-OF-BOUNDS INDEXES The indexes that are legal to access in an array are those in the range of 0 to the.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 17: Arrays for Tallying; Text Processing reading: 4.3, 7.6 (Slides adapted.
Topic 23 arrays - part 3 (tallying, text processing) Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Chapter 9 Introduction to Arrays Fundamentals of Java.
Copyright 2008 by Pearson Education Java Programing Array.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Lecture 18: Nested Loops and Two-Dimensional Arrays
CSCI 162 – Introduction to Programming II William Killian
Building Java Programs
Building Java Programs
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
CSC 142 Computer Science II
Building Java Programs
Building Java Programs
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
The for-loop and Nested loops
Building Java Programs
Building Java Programs
Lecture 7-3: Arrays for Tallying; Text Processing
Topic 23 arrays - part 3 (tallying, text processing)
Building Java Programs
Why did the programmer quit his job?
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
Building Java Programs Chapter 7
Building Java Programs
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
Lecture 13: Two-Dimensional Arrays
Building Java Programs
Building Java Programs
Array basics Readings: 7.1.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
A counting problem Problem: Write a method mostFrequentDigit that returns the digit value that occurs most frequently in a number. Example: The number.
Building Java Programs
Presentation transcript:

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 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 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(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): jag

Tallying Values with Arrays Problem: Write a method mostFrequentDigit that returns the digit value that occurs most frequently in a number. – Example: The number contains: one 0, two 2s, four 6es, one 7, and one 9. mostFrequentDigit( ) returns 6. – If there is a tie, return the digit with the lower value. mostFrequentDigit( ) 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 : – How do we build such an array? And how does it help? based on notes from Reges and Stepp, Building Java Programs index value

An Array of Tallies // assume n = 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 index value

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

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)); }

Exercise Output? 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);