Two-Dimensional Arrays. 15-110 Summer 2010 Margaret Reid-Miller



Similar documents
Arrays in Java. Working with Arrays

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

In this Chapter you ll learn:

CS170 Lab 11 Abstract Data Types & Objects

Answers to Review Questions Chapter 7

VB.NET Programming Fundamentals

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter 2: Elements of Java

AP Computer Science Java Subset

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Conditional Statements Summer 2010 Margaret Reid-Miller

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Iteration CHAPTER 6. Topic Summary

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

J a v a Quiz (Unit 3, Test 0 Practice)

Sample CSE8A midterm Multiple Choice (circle one)

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Stack Allocation. Run-Time Data Structures. Static Structures

AP Computer Science Java Mr. Clausen Program 9A, 9B

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 5. Recursion. Data Structures and Algorithms in Java

Using Two-Dimensional Arrays

Conditionals (with solutions)

Arrays in Java. data in bulk

Building Java Programs

Introduction to Java

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chapter 2 Introduction to Java programming

CMSC 202H. ArrayList, Multidimensional Arrays

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Building Java Programs

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

5 Arrays and Pointers

CS114: Introduction to Java

Building Java Programs

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

LAB4 Making Classes and Objects

Moving from C++ to VBA

Event-Driven Programming

(Eng. Hayam Reda Seireg) Sheet Java

Stack vs. Heap. Introduction to Programming. How the Stack Works. Primitive vs. Reference Types. Stack

Maria Litvin Phillips Academy, Andover, Massachusetts. Gary Litvin Skylight Publishing, Andover, Massachusetts

The resulting tile cannot merge with another tile again in the same move. When a 2048 tile is created, the player wins.

Java Classes. GEEN163 Introduction to Computer Programming

The Interface Concept

Abstraction and Abstract Data Types

Solving Problems Recursively

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Computer Science III Advanced Placement G/T [AP Computer Science A] Syllabus

Example of a Java program

>

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Supporting Data Set Joins in BIRT

Practical Session 4 Java Collections

D06 PROGRAMMING with JAVA

Variables, Constants, and Data Types

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

While and Do-While Loops Summer 2010 Margaret Reid-Miller

1. Writing Simple Classes:

8.1. Example: Visualizing Data

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

JAVA ARRAY EXAMPLE PDF

Project 2: Bejeweled

Moving from CS 61A Scheme to CS 61B Java

Object-Oriented Programming in Java

Computer Programming I

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Introduction to Java. CS 3: Computer Programming in Java

Lab Experience 17. Programming Language Translation

SmartArrays and Java Frequently Asked Questions

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Part I. Multiple Choice Questions (2 points each):

AP Computer Science A 2010 Free-Response Questions

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Lecture 11 Array of Linked Lists

Visualising Java Data Structures as Graphs

Topic 11 Scanner object, conditional execution

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Transcription:

Two-Dimensional Arrays 15-110 Margaret Reid-Miller

Two-Dimensional Arrays Arrays that we have consider up to now are onedimensional arrays, a single line of elements. Often data come naturally in the form of a table, e.g., spreadsheet, which need a two-dimensional array. Examples: Lab book of multiple readings over several days Periodic table Movie ratings by multiple reviewers. Each row is a different reviewer Each column is a different movie 2

Two-Dimensional Arrays Two-dimensional (2D) arrays are indexed by two subscripts, one for the row and one for the column. Example: row col rating[0][2] = 2!!rating[1][3] = 8! rating reviewer (first index) movie (second index) 0 1 2 3 0 4 6 2 5 1 7 9 4 8 2 6 9 3 7 3

Similarity with 1D Arrays Each element in the 2D array must by the same type, either a primitive type or object type Subscripted variables can be use just like a variable:! rating[0][3] = 10;! Array indices must be of type int and can be a literal, variable, or expression. rating[3][j] = j;! If an array element does not exists, the Java runtime system will give you an!!arrayindexoutofboundsexception 4

Declaring 2D Arrays Declare a local variable rating that references a 2D array of int: int[][] rating; Declare a field family that reference a 2D array of GiftCards: private GiftCard[][] family;! Create a 2D array with 3 rows and 4 columns and assign the reference to the new array to rating: rating = new int[3][4];! Shortcut to declare and create a 2D array: int[][] rating = new int[3][4]; 5

Example 1 Find the average rating by the reviewer in row 2. nt sum = 0;! for (int col = 0; col <= 3; col++) {!! sum += rating[2][col];! double average = (double) sum / 4;! reviewer 0 1 2 0 4 7 6 1 6 9 9 movie 2 3 2 5 4 8 3 7 6

Size of 2D Arrays When you write a method that has a 2D array as a parameter, how do you determine the size of the array? words! Hint: Consider a variable words, a 1D array of String references. What is the length of the array? What is the length of the word at index 2? 0 1 2 this! that! what! 7

2D Array Implementation A 2D array is a 1D array of (references to) 1D arrays. int[][] rating = new int[3][4];! 0 1 2 3 0 1 2 8

Size of 2D Arrays Given!!int[][] rating = new int[3][4]; What is the value of rating.length? Answer: 3, the number of rows (first dimension) What is the value of rating[0].length?! Answer: 4, the number of columns (second dimension) 9

Example 2 Find the number of ratings above the value of the parameter. public int countabove(int[][] rating, int num) {! int count = 0;! number of rows for (int row = 0; row < rating.length! ; row++) {! for (int col = 0; col < rating[0].length! ; col++) {!! if (rating[row][col] > num)! count++;! number of columns return count;! }! 10

Example 3 Print the average rating for the movie in column 3. int sum = 0;! reviewer 0 1 2 0 4 7 6 1 6 9 9 movie 2 3 2 5 4 8 3 7 for (int row = 0; row < rating.length ; row++) {!!sum += rating[ ][ ];! row 3! System.out.println((double) sum / rating.length );! 11

Ragged Arrays Since a 2D array is a 1D array of references to 1D arrays, each of these latter 1D arrays (rows) can have a different length. How? Use an initializer list. row 1 row 2!!int[][] rating = { {3,5,7,9}, {4,2},! {5,7,8,6}, {6} };! 3 5 7 9 4 2 5 7 8 6 6 12

Example 3 Revisited Print the average rating for the movie in column 3. int count = 0;! double sum = 0;! for (int row = 0; row < rating.length; row++) {!! if ( rating[row].length > 3) {! sum += rating[ ][ ];! row 3! count++;! 6 if (count > 0) {! System.out.println((double) sum / count);! 3 5 7 9 4 2 5 7 8 6 13

2D Array of Object References Recall that creating an array of object references fills the array with null values. Example: GiftCard[][] family = new GiftCard[3][4]! null 14

2D Array of Object References Need to create the objects and assign the references to the array elements. Example:!family[0][1] = new GiftCard( Macy s, 50.0);!!family[1][3] = new GiftCard( CVS, 15.0);! Macy s CVS null 15

Example 4 Print the total value of the gift cards for each family member (rows): printvalueofrows(family);! public static void printvalueofrows( GiftCard[][]! data) {! for (int row = 0; row < ; data.length row++) {! double total = ; 0.0! // find total for the row! for (int col = 0; col < ; data[row].length col++) {! if (data[row][col]!= ) null {! total += data[row][col].getbalance();! System.out.println( Row + row + : $ + total);! 16