Arrays in Java Multi-dimensional Arrays

Similar documents
ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Creating Two-dimensional Arrays

CSC 1051 Arrays - Review questions

Lesson 35..Two-Dimensional Arrays

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Arrays, Part 3: Multidimensional arrays

Lecture 13: Two- Dimensional Arrays

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

Introduction to Object-Oriented Programming

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Last Class. More on loops break continue A bit on arrays

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Chapter 9 Introduction to Arrays. Fundamentals of Java

CMSC131. Seating Chart

Two Dimensional Arrays

array Indexed same type

Array Basics: Outline

H212 Introduction to Software Systems Honors

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Arrays. Chapter 7. Walter Savitch Frank M. Carrano

Example: Computing prime numbers

STUDENT LESSON A12 Iterations

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

CS 230 Programming Languages

Exercise 4: Loops, Arrays and Files

Two-Dimensional Arrays

TOPICS TO COVER:-- Array declaration and use.

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

UNIT 6A Organizing Data: Lists Principles of Computing, Carnegie Mellon University

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Happy Cinco de Mayo!!!!

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

COS 126 General Computer Science Spring Written Exam 1

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

final int CHICAGO = 0; final int BOSTON = 1; final int MIAMI = 4;

Arrays and ArrayLists. David Greenstein Monta Vista High School

UNIT 6A Organizing Data: Lists. Last Two Weeks

Instructor: Eng.Omar Al-Nahal

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Happy Cinco de Mayo!!!!

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Collections

Casting. References. References

Introduction to Software Development (ISD) Week 3

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Chapter 6 Arrays, Part D 2d Arrays and the Arrays Class

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

AP Computer Science Lists The Array type

Chapter 7: Arrays CS 121. April 9, Department of Computer Science College of Engineering Boise State University. Chapter 7: Arrays CS / 41

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Simple Java Programming Constructs 4

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

Java Arrays. What could go wrong here? Motivation. Array Definition 9/28/18. Mohammad Ghafari

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

This page intentionally left blank

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Control Structures in Java if-else and switch

Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays

Arrays. Eng. Mohammed Abdualal

Multi-Dimensional Arrays and Vectors. Class 27

Arrays in Java Using Arrays

COMP 202. Programming With Arrays

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

EXAM Computer Science 1 Part 1

7.3 Arrays of Strings (Objects) 7.3 Arrays of Strings (Objects) 7.3 Tunes.java. 7.3 Arrays of Objects 9/11/13. ! A UML diagram for the Tunes program

All answers will be posted on web site, and most will be reviewed in class.

Chapter Introduction

AP Computer Science A Unit 7. Notes on Arrays

Arrays - Review. Two-Dimensional Arrays. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

Array. Prepared By - Rifat Shahriyar

Introduction to Object-Oriented Programming

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

CSC 1051 Algorithms and Data Structures I. Final Examination December 20, Name: KEY. Question Value Score

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Computer Science II Lecture 2 Strings, Vectors and Recursion

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Principles of Programming. Chapter 6: Arrays

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Transcription:

Suppose you are tasked with writing a program to help maintain seating records for a theatre company. The auditorium has 25 rows, each of which contains 30 seats. One utility you need to provide is tracking which seats are sold, which can be accomplished using boolean values. We could use a single array for each row, which would require the following declaration: boolean[] row1 = new boolean[30]; boolean[] row2 = new boolean[30];... boolean[] row25 = new boolean[30]; Each row could be visualized as row1 0 1 2 29 f f f f Obviously this approach requires too much manual declaration of each row, and the program becomes difficult to maintain even if simple changes are made to the number of rows or the number of seats. Fortunately, Java allows the declaration of multi-dimensional arrays. boolean[][] sold = new boolean[25][30]; The results of the declaration are shown the following diagram: sold 0 1 2 29 sold[0] f f f f sold[1] f f f f sold[24] f f f f This diagram may somewhat surprising, but it is consistent with our previous considerations of a onedimensional array. If we begin with the declaration boolean[] sold = new boolean[25]; (3) (2) (1) we are declaring a reference (1) to an array object (2) that contains boolean elements (3). 16. Nov. 2010 Page 1 of 9

Now consider the declaration of our two-dimensional array. boolean[][] sold = new boolean[25][30]; (3) (2) (1) By adding the second set of square brackets, we have declared a reference variable (1) to an array object (2) that contains references to arrays of boolean elements. Specifically, we have 25 arrays of 30 boolean elements. Often, we use two-dimensional arrays to represent values in a simple rectangular table. In such cases, we can think of the two-dimensional array as consisting of rows (horizontal) and columns (vertical), like a spreadsheet. If we do this, it is customary to think of the row index coming before the column index. boolean[][] sold = new boolean[rows][columns]; Traversing a Two-dimensional Array Just as the for statement is useful for traversing a one-dimensional array, we can use a nested for statement to traverse a two-dimensional array. int[][] table = new int[3][6]; will create an array with 3 rows and 6 columns, and by default each element will automatically be assigned the value of zero. columns 0 1 2 3 4 5 0 0 0 0 0 0 0 rows 1 0 0 0 0 0 0 2 0 0 0 0 0 0 If we wished to initialize the array elements to one, we could use the following code fragment: for (int row = 0; row < table.length; row++) for (int col = 0; col < table[row].length; col++) table[row][col] = 1; Initializing a Two-dimensional Array at Declaration Suppose we want to create a table with 3 rows and 4 columns, initialized as follows: 5 2 7 3 6 8 7 9 4 5 2 3 16. Nov. 2010 Page 2 of 9

We can do so with the following statement: int[][] scores = 5,2,7,3, 6,8,7,9, 4,5,2,3; Arrays in Java Multi-dimensional Arrays Notice that we have used multiple lines to represent the statement. This is strictly for readability. The equivalent statement, on a single line, would work equally well. int[][] scores = 5,2,7,3, 6,8,7,9, 4,5,2,3; Non-rectangular Two-dimensional Arrays The one-dimensional arrays that compose a two-dimensional array do not need to be the same length, although this is certainly the most common configuration. Consider the following triangular array: 0 0 columns 0 1 2 3 4 1 0 0 rows 2 0 0 0 3 0 0 0 0 4 0 0 0 0 0 In our declaration of such an array, we cannot specify the number of columns for each, since that size changes with each row. Thus we leave the number of columns blank for now. double[][] tritable = new double[5][]; Now we must dynamically create the array for each row to a custom size, which can be accomplished with the following code fragment: for (int row = 0; row < tritable.length; row++) tritable[row] = new double[row+1]; Note: Array declarations such as the one above, where the size of the second dimension is left blank, are legal. However, it is not legal to have the first dimension unspecified. int[][] badarray = new int[][20]; // generates an error, will not compile Non-rectangular two-dimensional arrays are sometimes called ragged arrays. 16. Nov. 2010 Page 3 of 9

Initializing a Ragged Array int[][] ragged = 4, 3, 7, 5, 2, 7, 8, 1, 4; creates the array whose elements are illustrated below. Traversing a Ragged Array 4 3 7 5 2 7 8 1 4 In traversing a ragged two-dimensional array, we cannot assume that each row contains the same numbers of columns. As such, our nested for loops must be slightly modified. Suppose we want to find the sum of the elements in the ragged array declared above. int sum = 0; for (int row = 0; row < ragged.length; row++) for (int col = 0; col < ragged[row].length; col++) sum += ragged[row][col]; 16. Nov. 2010 Page 4 of 9

Exercises 1. The diagram shows an array declared by the statement int[][] a = new int[3][4]; State the identifier of each cell marked by a letter. A B C D E F 2. How many elements would be in each of the arrays created by the following declarations? a) double[][] first = new double[25][40]; b) boolean [][][] second = new boolean[3][6][50]; c) char [][] third = new char[60][40]; 3. Suppose that the following declarations have been made: int a[][] = 4,2,7, 3,9,1; Determine what would be printed by each fragment a) for (i = 0; i < a.length; i++) for (j = 0; j < a[0].length; j++) System.out.print(a[i][j]); System.out.println(); b) for (i = 0; i < a[0].length; i++) for (j = 0; j < a.length; j++) System.out.print(a[j][i]); System.out.println(); 16. Nov. 2010 Page 5 of 9

c) for (i = a.length - 1; i >= 0; i--) for (j = 0; j < a[0].length; j++) System.out.print(a[i][j]); System.out.println(); 4. For the array given in the previous question, write a fragment that would print the elements of the array in the form 17 92 34 5. Write a method sum having one double[][] parameter. The method should return the sum of the elements of the array passed to it. You may assume that the array is rectangular. 6. Write a method max that will return the maximum value of the elements in a two-dimensional array of int values. Do not assume that the array is rectangular. 7. Write a method size that has one int[][][] parameter. The method should return the number of elements in the array. Do not make any assumptions about regularity of the array. 16. Nov. 2010 Page 6 of 9