Java Programming: FromProblem Analysis to Program Design, 5e 2
Declaring Arrays as Formal Parameters to Methods
• A general syntax to declare an array as a formal parameter
public static dataType arraysAsFormalParameter(dataType[]
arrayName1, dataType[] arrayName2, . . . )
{
//...
}
Arrays & Methods
3.
Java Programming: FromProblem Analysis to Program Design, 5e 3
Example: Printing elements of an array
Arrays & Methods
Another print array method:
4.
Java Programming: FromProblem Analysis to Program Design, 5e 4
Example: Reading values into an array
Arrays & Methods
5.
5
Example: Creating acopy of an array or part of an array
Arrays & Methods
public static void copyArray(int[] dataArray1, int[]
dataArray2, int start, int numOfElements) {
for (int index = 0; index < numOfElements; index++){
dataArray2[index] = dataArray1[start];
start++;
}
}
6.
Java Programming: FromProblem Analysis to Program Design, 5e 6
Example: Computing the sum of an array
Arrays & Methods
7.
Java Programming: FromProblem Analysis to Program Design, 5e 7
Example: Computing the largest element in an array
Arrays & Methods
8.
8
Public static booleanareEqualArrays(int[] firstArray,
int[] secondArray)
{
if (firstArray.length != secondArray.length)
return false;
for (int index = 0; index < firstArray.length;index++)
if (firstArray[index] != secondArray[index])
return false;
return true;
}
Using the Method
if (areEqualArrays(dataArrayA, dataArrayB))
...
Relational Operators and Arrays
9.
Java Programming: FromProblem Analysis to Program Design, 5e 9
• Suppose that you want to determine whether 27 is in the
dataArray
• First you compare 27 with dataArray[0]
• Because dataArray[0] ≠ 27, you then compare 27 with
dataArray[1]
• Because dataArray[1] ≠ 27, you compare 27 with
dataArray[2]; because dataArray[2] = 27, the search
stops
• This search is successful
Searching an Array for Specific Item
10.
Java Programming: FromProblem Analysis to Program Design, 5e 10
• Search for 10
• Search starts at the first element in the dataArray, that is, at
dataArray[0]
• This time, the search item, which is 10, is compared with every item
in the dataArray; eventually, no more data is left in the dataArray to
compare with the search item; this is an unsuccessful search
Searching an Array for Specific Item
11.
11
public static intsequentialSearch( int [] dataArray,
int searchItem ){
int loc;
for(loc = 0; loc < dataArray.length; loc++){
if(dataArray[loc] == searchItem)
return loc;
}
return -1;
}
A Methods to Search an Array Sequentially – Method 1
12.
12
public static intsequentialSearch(int[] dataArray,
int searchItem) {
int loc;
loc = 0;
while (loc < dataArray.length){
if (dataArray[loc] == searchItem)
return loc;
else
loc++;
}
return -1;
}
A Methods to Search an Array Sequentially – Method 2
13.
Example (Sum ofarray elements)
Java Programming: From Problem Analysis to Program Design, 5e 13
14.
Example (Sum ofarray elements) and print
using Arrays.toString method to print it
Java Programming: From Problem Analysis to Program Design, 5e 14
15.
Example (max elementin an array)
Java Programming: From Problem Analysis to Program Design, 5e 15
Java Programming: FromProblem Analysis to Program Design, 5e 18
double[][] sales = new double[10][5];
Two-Dimensional Arrays
19.
Java Programming: FromProblem Analysis to Program Design, 5e 19
• intExp1, intExp2 >= 0
• indexExp1 = row position
• indexExp2 = column position
Two-Dimensional Arrays - Accessing Array Elements
Java Programming: FromProblem Analysis to Program Design, 5e 21
• This statement declares and instantiates a two-dimensional array
matrix of 20 rows and 15 columns
• The value of the expression:
matrix.length
is 20, the number of rows
Two-Dimensional Arrays and the Instance Variable length
22.
Java Programming: FromProblem Analysis to Program Design, 5e 22
• Each row of matrix is a one-dimensional array; matrix[0], in fact,
refers to the first row
• The value of the expression:
matrix[0].length
is 15, the number of columns in the first row
• matrix[1].length gives the number of columns in the second
row, which in this case is 15, and so on
Two-Dimensional Arrays and the Instance Variable length
23.
Java Programming: FromProblem Analysis to Program Design, 5e 23
Two-Dimensional Arrays: Special Cases
25
Two-Dimensional Array Initializationduring Declaration
• To initialize a two-dimensional array when it is declared:
- The elements of each row are enclosed within braces and separated
by commas
- All rows are enclosed within braces
26.
Java Programming: FromProblem Analysis to Program Design, 5e 26
Two-Dimensional Array Initialization during Declaration
27.
27
• Three waysto process 2D arrays
– Entire array
– Particular row of array (row processing)
– Particular column of array (column processing)
• Processing algorithms are similar to processing algorithms of one-
dimensional arrays
Two-Dimensional Arrays: Processing
28.
Java Programming: FromProblem Analysis to Program Design, 5e 28
Initialization
for (int row = 0; row < matrix.length; row++)
for (int col = 0; col < matrix[row].length; col++)
matrix[row][col] = 10;
Two-Dimensional Arrays: Processing
29.
Java Programming: FromProblem Analysis to Program Design, 5e 29
Printing 2D Array
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[row].length; col++)
System.out.printf("%7d", matrix[row][col]);
System.out.println();
}
Two-Dimensional Arrays: Processing
30.
Java Programming: FromProblem Analysis to Program Design, 5e 30
Input into 2D Array
for (int row = 0; row < matrix.length; row++)
for (int col = 0; col < matrix[row].length; col++)
matrix[row][col] = console.nextInt();
Two-Dimensional Arrays: Processing
31.
31
Sum of AllElements in a 2D Array
sum = 0;
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[row].length; col++)
sum = sum + matrix[row][col];
}
System.out.println("Sum of row " + row + " = “ + sum);
Two-Dimensional Arrays: Processing
32.
32
Sum by Row
for(int row = 0; row < matrix.length; row++)
{
sum = 0;
for (int col = 0; col < matrix[row].length; col++)
sum = sum + matrix[row][col];
System.out.println("Sum of row " + row + " = “ + sum);
}
Two-Dimensional Arrays: Processing
33.
Java Programming: FromProblem Analysis to Program Design, 5e 33
Sum by Column
for (int col = 0; col < matrix[col].length; col++)
{
sum = 0;
for (int row = 0; row < matrix.length; row++)
sum = sum + matrix[row][col];
System.out.println("Sum of column " + col + " = " + sum);
}
Two-Dimensional Arrays: Processing
34.
34
Largest Element ina 2D Array
largest = matrix[0][0];
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[row].length; col++)
if (largest < matrix[row][col])
largest = matrix[row][col];
}
System.out.println(“Largest element: “ + largest);
Two-Dimensional Arrays: Processing
35.
35
Two-Dimensional Arrays asParameters to methods
Passing 2D Arrays as Parameters to methods
Printing an Array
public static void printMatrix(int[][] matrix)
{
for (int row = 0; row < matrix.length; row++)
for (int col = 0; col < matrix[row].length; col++)
System.out.printf("%7d", matrix[row][col]);
System.out.println();
}
36.
36
Two-Dimensional Arrays asParameters to methods
Method to compute the sum of a 2D Array
public static int sumMatrix(int[][] matrix){
sum = 0;
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[row].length; col++)
sum = sum + matrix[row][col];
}
return sum;
}
37.
37
Two-Dimensional Arrays asParameters to methods
Method to compute largest element in a 2D Array
Public static int largestMatrix(int[][] matrix){
int largest = matrix[0][0];
for (int row = 0; row < matrix.length; row++){
for (int col = 0; col < matrix[row].length; col++){
if (largest < matrix[row][col])
largest = matrix[row][col];
}
}
return largest;
}
38.
38
Two-Dimensional Arrays asParameters to methods- Full Program
/** Largest Element in Matrix **/
public class largestMatrixElement {
public static void main( String [] args ){
// define an array
int [][] myData = { {3, 6, 32, 69, 72},
{89, 43, 95, 7, 8},
{12, 15, 22, 36, 45},
{48, 78, 51, 53, 19}
};
int maxElement;
maxElement = largestMatrix(myData);
System.out.println("Largest value: " + maxElement );
}
39.
39
Two-Dimensional Arrays asParameters to methods- Full Program
public static int largestMatrix(int[][] matrix){
int largest = matrix[0][0];
for (int row = 0; row < matrix.length; row++){
for (int col = 0; col < matrix[row].length; col++){
if (largest < matrix[row][col])
largest = matrix[row][col];
}
}
return largest;
}
}
40.
40
Class Exercises -#1
The following statements computes the minimum value in a 2D Array:
int [][] a = {{9, 8, 7, 6},
{10, 20, 30, 40} };
int min = a[0][0];
for (int i = 1; i < a.length; i++){
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] < min)
min = [i][j];
}
}
System.out.println(“The minimum is ” + min);
Is the output correct?
41.
41
Class Exercises -#1 Correction
The following statements computes the minimum value in a 2D Array:
int [][] a = {{9, 8, 7, 6},
{10, 20, 30, 40} };
int min = a[0][0];
for (int i = 1; i < a.length; i++){
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] < min)
min = a[i][j];
}
}
System.out.println(“The minimum is ” + min);
43
Class Exercises -#2
Write a Java method that returns the number of rows that have two columns.
44.
44
Class Exercises -#2 - Solution
Write a Java method that returns the number of rows that have
two columns.
public static int rowsWithTwoColumns (int [][] a) {
int countRows;
int row;
countRows = 0;
for (row = 0; row < a.length; row++){
if (a[row].length == 2)
countRows ++;
}
return countRows;
}
45.
45
Class Exercises -#3
Write a Java method that returns the number of columns in each row.
46.
46
Class Exercises -#3 - Solution
Write a Java method that returns the number of columns in each row.
public static int[] columnsInRows (int [][] a) {
int[] numColumns = new int [a.length];
int row;
for (row = 0; row < a.length; row++){
numColumns[row] = a[row].length;
}
return numColumns;
}