Multidimensional Arrays Section 6.4

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Arrays.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
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.
Java Unit 9: Arrays Declaring and Processing Arrays.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CMSC 202 Java Primer. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays Chapter 7.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Pointers and Dynamic Arrays
Arrays Chapter 7.
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Two-Dimensional Arrays
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Parameter Lists & Command Line Arguments
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Java How to Program, Late Objects Version, 10/e
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Chapter 14 Graphs and Paths.
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 8 Slides from GaddisText
Chapter 10 Datapath Subsystems.
Interfaces and Inner Classes
Multidimensional Arrays
Chapter 20 Hash Tables.
Chapter 7 Part 2 Edited by JJ Shepherd
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays Chapter 7.
Chapter 6 Data Types.
Multi-Dimensional Arrays
The Facts to Be Explained
Outline Declaring and Using Arrays Arrays of Objects
Chapter 4 Constructors Section 4.4
Information Hiding and Encapsulation Section 4.2
C++ Array 1.
Circuit Characterization and Performance Estimation
Chapter 6 Arrays.
Chapter 6 Dynamic Programming.
Chapter 2 Reference Types.
Chapter 4 Greedy Algorithms.
Arrays.
Presentation transcript:

Multidimensional Arrays Section 6.4 Chapter 6 Multidimensional Arrays Section 6.4 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage

Multidimensional Arrays It is sometimes useful to have an array with more than one index Multidimensional arrays are declared and created in basically the same way as one-dimensional arrays You simply use as many square brackets as there are indices Each index must be enclosed in its own brackets double[][]table = new double[100][10]; int[][][] figure = new int[10][20][30]; Person[][] = new Person[10][100]; Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Multidimensional Arrays Multidimensional arrays may have any number of indices, but perhaps the most common number is two Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row, and the second index giving the column char[][] a = new char[5][12]; Note that, like a one-dimensional array, each element of a multidimensional array is just a variable of the base type (in this case, char) Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Multidimensional Arrays In Java, a two-dimensional array, such as a, is actually an array of arrays The array a contains a reference to a one-dimensional array of size 5 with a base type of char[] Each indexed variable (a[0], a[1], etc.) contains a reference to a one-dimensional array of size 12, also with a base type of char[] A three-dimensional array is an array of arrays of arrays, and so forth for higher dimensions Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Two-Dimensional Array as an Array of Arrays (Part 1 of 2) Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Two-Dimensional Array as an Array of Arrays (Part 2 of 2) Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Using the length Instance Variable char[][] page = new char[30][100]; The instance variable length does not give the total number of indexed variables in a two-dimensional array Because a two-dimensional array is actually an array of arrays, the instance variable length gives the number of first indices (or "rows") in the array page.length is equal to 30 For the same reason, the number of second indices (or "columns") for a given "row" is given by referencing length for that "row" variable page[0].length is equal to 100 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Using the length Instance Variable The following program demonstrates how a nested for loop can be used to process a two-dimensional array Note how each length instance variable is used int row, column; for (row = 0; row < page.length; row++) for (column = 0; column < page[row].length; column++) page[row][column] = 'Z'; Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Ragged Arrays Each row in a two-dimensional array need not have the same number of elements Different rows can have different numbers of columns An array that has a different number of elements per row it is called a ragged array Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Ragged Arrays double[][] a = new double[3][5]; The above line is equivalent to the following: double [][] a; a = new double[3][]; //Note below a[0] = new double[5]; a[1] = new double[5]; a[2] = new double[5]; Note that the second line makes a the name of an array with room for 3 entries, each of which can be an array of doubles that can be of any length The next 3 lines each create an array of doubles of size 5 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Ragged Arrays double [][] a; a = new double[3][]; Since the above line does not specify the size of a[0], a[1], or a[2], each could be made a different size instead: a[0] = new double[5]; a[1] = new double[10]; a[2] = new double[4]; Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Multidimensional Array Parameters and Returned Values Methods may have multidimensional array parameters They are specified in a way similar to one-dimensional arrays They use the same number of sets of square brackets as they have dimensions public void myMethod(int[][] a) { . . . } The parameter a is a two-dimensional array Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Multidimensional Array Parameters and Returned Values Methods may have a multidimensional array type as their return type They use the same kind of type specification as for a multidimensional array parameter public double[][] aMethod() { . . . } The method aMethod returns a two-dimensional array of double Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. A Grade Book Class As an example of using arrays in a program, a class GradeBook is used to process quiz scores Objects of this class have three instance variables grade: a two-dimensional array that records the grade of each student on each quiz studentAverage: an array used to record the average quiz score for each student quizAverage: an array used to record the average score for each quiz Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. A Grade Book Class The score that student 1 received on quiz number 3 is recorded in grade[0][2] The average quiz grade for student 2 is recorded in studentAverage[1] The average score for quiz 3 is recorded in quizAverage[2] Note the relationship between the three arrays Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The Two-Dimensional Array grade Copyright © 2012 Pearson Addison-Wesley. All rights reserved.