Java Programming: From Problem
Analysis to Program Design, 5e
Arrays & Methods
Arrays & Methods
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, . . . )
{
//...
}
Java Programming: From Problem Analysis to Program Design, 5e 2
Arrays & Methods
Example: Printing elements of an array
Another print array method:
Java Programming: From Problem Analysis to Program Design, 5e 3
Arrays & Methods
Example: Reading values into an array
Java Programming: From Problem Analysis to Program Design, 5e 4
Arrays & Methods
Example: Creating a copy of an array or part of an array
public static void copyArray(int[] dataArray1, int[]
dataArray2, int start, int numOfElements) {
for (int index = 0; index < numOfElements; index++){
dataArray2[index] = dataArray1[start];
start++;
}
}
5
Arrays & Methods
Example: Computing the sum of an array
Java Programming: From Problem Analysis to Program Design, 5e 6
Arrays & Methods
Example: Computing the largest element in an array
Java Programming: From Problem Analysis to Program Design, 5e 7
Relational Operators and Arrays
Public static boolean areEqualArrays(int[] firstArray,
int[] secondArray)
{
if ([Link] != [Link])
return false;
for (int index = 0; index < [Link];index++)
if (firstArray[index] != secondArray[index])
return false;
return true;
}
Using the Method
if (areEqualArrays(dataArrayA, dataArrayB))
...
8
Searching an Array for Specific Item
• 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
Java Programming: From Problem Analysis to Program Design, 5e 9
Searching an Array for Specific Item
• 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
Java Programming: From Problem Analysis to Program Design, 5e 10
A Methods to Search an Array Sequentially – Method 1
public static int sequentialSearch( int [] dataArray,
int searchItem ){
int loc;
for(loc = 0; loc < [Link]; loc++){
if(dataArray[loc] == searchItem)
return loc;
}
return -1;
11
A Methods to Search an Array Sequentially – Method 2
public static int sequentialSearch(int[] dataArray,
int searchItem) {
int loc;
loc = 0;
while (loc < [Link]){
if (dataArray[loc] == searchItem)
return loc;
else
loc++;
}
return -1;
}
12
Example (Sum of array elements)
Java Programming: From Problem Analysis to Program Design, 5e 13
Example (Sum of array elements) and print
using [Link] method to print it
Java Programming: From Problem Analysis to Program Design, 5e 14
Example (max element in an array)
Java Programming: From Problem Analysis to Program Design, 5e 15
Two-Dimensional Arrays
Java Programming: From Problem Analysis to Program Design, 5e 16
Two-Dimensional Arrays
Java Programming: From Problem Analysis to Program Design, 5e 17
Two-Dimensional Arrays
double[][] sales = new double[10][5];
Java Programming: From Problem Analysis to Program Design, 5e 18
Two-Dimensional Arrays - Accessing Array Elements
• intExp1, intExp2 >= 0
• indexExp1 = row position
• indexExp2 = column position
Java Programming: From Problem Analysis to Program Design, 5e 19
Two-Dimensional Arrays - Accessing Array Elements
20
Two-Dimensional Arrays and the Instance Variable length
• This statement declares and instantiates a two-dimensional array
matrix of 20 rows and 15 columns
• The value of the expression:
[Link]
is 20, the number of rows
Java Programming: From Problem Analysis to Program Design, 5e 21
Two-Dimensional Arrays and the Instance Variable length
• 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
Java Programming: From Problem Analysis to Program Design, 5e 22
Two-Dimensional Arrays: Special Cases
Java Programming: From Problem Analysis to Program Design, 5e 23
Two-Dimensional Arrays: Special Cases
• Create columns
24
Two-Dimensional Array Initialization during 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
25
Two-Dimensional Array Initialization during Declaration
Java Programming: From Problem Analysis to Program Design, 5e 26
Two-Dimensional Arrays: Processing
• Three ways to 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
27
Two-Dimensional Arrays: Processing
Initialization
for (int row = 0; row < [Link]; row++)
for (int col = 0; col < matrix[row].length; col++)
matrix[row][col] = 10;
Java Programming: From Problem Analysis to Program Design, 5e 28
Two-Dimensional Arrays: Processing
Printing 2D Array
for (int row = 0; row < [Link]; row++)
for (int col = 0; col < matrix[row].length; col++)
[Link]("%7d", matrix[row][col]);
[Link]();
Java Programming: From Problem Analysis to Program Design, 5e 29
Two-Dimensional Arrays: Processing
Input into 2D Array
for (int row = 0; row < [Link]; row++)
for (int col = 0; col < matrix[row].length; col++)
matrix[row][col] = [Link]();
Java Programming: From Problem Analysis to Program Design, 5e 30
Two-Dimensional Arrays: Processing
Sum of All Elements in a 2D Array
sum = 0;
for (int row = 0; row < [Link]; row++)
for (int col = 0; col < matrix[row].length; col++)
sum = sum + matrix[row][col];
[Link]("Sum of row " + row + " = “ + sum);
31
Two-Dimensional Arrays: Processing
Sum by Row
for (int row = 0; row < [Link]; row++)
sum = 0;
for (int col = 0; col < matrix[row].length; col++)
sum = sum + matrix[row][col];
[Link]("Sum of row " + row + " = “ + sum);
32
Two-Dimensional Arrays: Processing
Sum by Column
for (int col = 0; col < matrix[col].length; col++)
sum = 0;
for (int row = 0; row < [Link]; row++)
sum = sum + matrix[row][col];
[Link]("Sum of column " + col + " = " + sum);
Java Programming: From Problem Analysis to Program Design, 5e 33
Two-Dimensional Arrays: Processing
Largest Element in a 2D Array
largest = matrix[0][0];
for (int row = 0; row < [Link]; row++)
{
for (int col = 0; col < matrix[row].length; col++)
if (largest < matrix[row][col])
largest = matrix[row][col];
}
[Link](“Largest element: “ + largest);
34
Two-Dimensional Arrays as Parameters to methods
Passing 2D Arrays as Parameters to methods
Printing an Array
public static void printMatrix(int[][] matrix)
for (int row = 0; row < [Link]; row++)
for (int col = 0; col < matrix[row].length; col++)
[Link]("%7d", matrix[row][col]);
[Link]();
35
Two-Dimensional Arrays as Parameters to methods
Method to compute the sum of a 2D Array
public static int sumMatrix(int[][] matrix){
sum = 0;
for (int row = 0; row < [Link]; row++)
{
for (int col = 0; col < matrix[row].length; col++)
sum = sum + matrix[row][col];
}
return sum;
}
36
Two-Dimensional Arrays as Parameters 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 < [Link]; row++){
for (int col = 0; col < matrix[row].length; col++){
if (largest < matrix[row][col])
largest = matrix[row][col];
}
}
return largest;
}
37
Two-Dimensional Arrays as Parameters 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);
[Link]("Largest value: " + maxElement );
} 38
Two-Dimensional Arrays as Parameters to methods- Full Program
public static int largestMatrix(int[][] matrix){
int largest = matrix[0][0];
for (int row = 0; row < [Link]; row++){
for (int col = 0; col < matrix[row].length; col++){
if (largest < matrix[row][col])
largest = matrix[row][col];
}
}
return largest;
}
}
39
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 < [Link]; i++){
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] < min)
min = [i][j];
}
}
[Link](“The minimum is ” + min);
Is the output correct?
40
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 < [Link]; i++){
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] < min)
min = a[i][j];
}
}
[Link](“The minimum is ” + min);
41
Class Exercises - #1 complete code
42
Class Exercises - #2
Write a Java method that returns the number of rows that have two columns.
43
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 < [Link]; row++){
if (a[row].length == 2)
countRows ++;
}
return countRows;
}
44
Class Exercises - #3
Write a Java method that returns the number of columns in each row.
45
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 [[Link]];
int row;
for (row = 0; row < [Link]; row++){
numColumns[row] = a[row].length;
}
return numColumns;
}
46
Class Exercises - #3 – Complete Program
47
Class Exercises - #3 – Complete Program Solution 2 (void method)
48