0% found this document useful (0 votes)
31 views1 page

2DArray Algorithms

The document contains two Java methods for working with a 2D array. The first method, countIn2DArray, counts the occurrences of a specified target value in the array. The second method, searchIn2DArray, finds and returns the coordinates of the target value within the array.

Uploaded by

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

2DArray Algorithms

The document contains two Java methods for working with a 2D array. The first method, countIn2DArray, counts the occurrences of a specified target value in the array. The second method, searchIn2DArray, finds and returns the coordinates of the target value within the array.

Uploaded by

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

public static int countIn2DArray(int[][] twoDArray, int target)

{
int count = 0;
for (int row = 0; row < twoDArray.length; row++) {
for (int i = 0; i < twoDArray[row].length; i++) {
if (twoDArray[row][i] == target)
count++;
}
}
return count;
}

public static String searchIn2DArray(int[][] twoDArray, int target)


{
int targetRow = -1;
int targetColumn = -1;
for (int row = 0; row < twoDArray.length; row++) {
for (int i = 0; i < twoDArray[row].length; i++) {
if (twoDArray[row][i] == target) {
targetRow = row;
targetColumn = i;
}
}
}
return "The coordinates of the target value is " + targetRow + ", " + targetColumn;
}

You might also like