0% found this document useful (0 votes)
8 views18 pages

Multidimensional Arrays in Java

Chapter 8 of 'Introduction to Java Programming and Data Structures' focuses on multidimensional arrays, particularly two-dimensional arrays, which can represent matrices or tables. The chapter covers how to declare, create, and manipulate these arrays, including operations such as summing elements and checking Sudoku solutions. It also introduces ragged arrays and provides examples of initializing and printing arrays.

Uploaded by

amohimeed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views18 pages

Multidimensional Arrays in Java

Chapter 8 of 'Introduction to Java Programming and Data Structures' focuses on multidimensional arrays, particularly two-dimensional arrays, which can represent matrices or tables. The chapter covers how to declare, create, and manipulate these arrays, including operations such as summing elements and checking Sudoku solutions. It also introduces ragged arrays and provides examples of initializing and printing arrays.

Uploaded by

amohimeed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Introduction to Java Programming and

Data Structures
Thirteenth Edition

Chapter 8
Multidimensional Arrays

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Motivations (1 of 2)
Thus far, you have used one-dimensional arrays to model linear collections of
elements. You can use a two-dimensional array to represent a matrix or a table.
For example, the following table that describes the distances between the cities
can be represented using a two-dimensional array.

Distance Table (in miles)


Chicago Boston New York Atlanta Miami Dallas Houston
Blank

Chicago 0 983 787 714 1375 967 1087


Boston 983 0 214 1102 1763 1723 1842
New York 787 214 0 888 1549 1548 1627
Atlanta 714 1102 888 0 661 781 810
Miami 1375 1763 1549 661 0 1426 1187
Dallas 967 1723 1548 781 1426 0 239
Houston 1087 1842 1627 810 1187 239 0

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Motivations (2 of 2)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Objectives (1 of 2)
8.1 To give examples of representing data using two-
dimensional arrays (§8.1).
8.2 To declare variables for two-dimensional arrays, create
arrays, and access array elements in a two-dimensional
array using row and column indexes (§8.2).
8.3 To program common operations for two-dimensional
arrays (displaying arrays, summing all elements, finding the
minimum and maximum elements, and random shuffling)
(§8.3).
8.4 To pass two-dimensional arrays to methods (§8.4).

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Objectives (2 of 2)
8.5 To write a program for grading multiple-choice
questions using two-dimensional arrays (§8.5).
8.6 To solve the closest-pair problem using two-
dimensional arrays (§8.6).
8.7 To check a Sudoku solution using two-dimensional
arrays (§8.7).
8.8 To use multidimensional arrays (§8.8).

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Declare/Create Two-dimensional Arrays
// Declare array ref var
dataType[][] refVar;
// Create array and assign its reference to
variable
refVar = new dataType[10][10];
// Combine declaration and creation in one
statement
dataType[][] refVar = new dataType[10][10];
// Alternative syntax
dataType refVar[][] = new dataType[10][10];

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Declaring Variables of Two-dimensional Arrays
and Creating Two-dimensional Arrays

int[][] matrix = new int[10][10];


or
int matrix[][] = new int[10][10];
matrix[0][0] = 3;
for (int i = 0; i < [Link]; i++)
for (int j = 0; j < matrix[i].length; j++)
matrix[i][j] = (int)([Link]() *
1000);
double[][] x;
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Two-dimensional Array Illustration

[Link]? 5 [Link]? 4
matrix[0].length? 5 array[0].length? 3
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Declaring, Creating, and Initializing Using
Shorthand Notations

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Lengths of Two-dimensional Arrays (1 of 2)
int[][] x = new int[3][4];

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Lengths of Two-dimensional Arrays (2 of 2)
int[][] array = { [Link]
{1, 2, 3}, array[0].length
{4, 5, 6}, array[1].length
{7, 8, 9}, array[2].length
{10, 11, 12} array[3].length
};

array[4].length ArrayIndexOutOfBoundsException

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Ragged Arrays (1 of 2)
Each row in a two-dimensional array is
itself an array. So, the rows can have
different lengths. Such an array is
known as a ragged array. For example,
int[][] matrix = {
[Link] is 5
{1, 2, 3, 4, 5},
{2, 3, 4, 5}, matrix[0].length is 5

{3, 4, 5}, matrix[1].length is 4

{4, 5}, matrix[2].length is 3


{5} matrix[3].length is 2
}; matrix[4].length is 1

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Ragged Arrays (2 of 2)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Initializing Arrays With Input Values
[Link] input = new Scanner([Link]);
[Link]("Enter " + [Link] + " rows and " +
matrix[0].length + " columns: ");
for (int row = 0; row < [Link]; row++) {
for (int column = 0; column < matrix[row].length; column+
+) {
matrix[row][column] = [Link]();
}
}
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Printing Arrays
for (int row = 0; row < [Link]; row++) {
for (int column = 0; column < matrix[row].length; column+
+) {
[Link](matrix[row][column] + " ");
}
[Link]();
}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Summing All Elements
int total = 0;
for (int row = 0; row < [Link]; row++) {
for (int column = 0; column < matrix[row].length; column+
+) {
total += matrix[row][column];
}
}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Summing Elements by Column
for (int column = 0; column < matrix[0].length; column++) {
int total = 0;
for (int row = 0; row < [Link]; row++)
total += matrix[row][column];
[Link]("Sum for column " + column + " is "
+ total);
}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Copyright

This work is protected by United States copyright laws and is


provided solely for the use of instructors in teaching their
courses and assessing student learning. Dissemination or sale of
any part of this work (including on the World Wide Web) will
destroy the integrity of the work and is not permitted. The work
and materials from it should never be made available to students
except by instructors using the accompanying text in their
classes. All recipients of this work are expected to abide by these
restrictions and to honor the intended pedagogical purposes and
the needs of other instructors who rely on these materials.

Copyright © 2024 Pearson Education, Inc. All Rights Reserved

You might also like