# How to Find Minimum Value in 2d Array Java

How to Find Minimum Value in 2d Array Java | The 2d array is a two-dimensional matrix. On this page, we will find the minimum value in the two-dimensional matrix. In order to do this, we need to create a method as there is no built-in method or function to find the minimum value in the given matrix.

See the below examples to may get more ideas about the problem.

Example-1:-
Array = { { 1, 2 }, { 4, 5 }, { 7, 8 } };
Minimum value in 2d array = 1

Example-2:-
Array = { { 100, 105, 300 }, { 104, 50, 603 }, { 745, 813, 999 } };
Minimum value in 2d array = 50

Example-3:-
Array = {{ }};
There are no elements in the array.

If there are no elements in the array that is if the row value or column value is equal to 0 then the code returns “there are no elements in the array”.

## Find Minimum Value in 2d Array Java

Now let us see a program to find minimum value in 2d array Java. Note:- We are assuming that the input array will not be a jagged array.

``````import java.util.Scanner;
import java.util.Arrays;

class Main {
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of elements in a row: ");
int n = scan.nextInt();
System.out.print("Enter the number of elements in a column: ");
int m = scan.nextInt();
int[][] array = new int[n][m];

if (n == 0 || m == 0) {
System.out.println("There are no elements in the"+
" given 2d array.");
} else {
System.out.println("Enter the elements for the 2d array: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
array[i][j] = scan.nextInt();
}
}

System.out.println("2D array = " +
Arrays.deepToString(array));
System.out.println("The minimum value in the given"+
" 2d array is: " + getMinimumIn2DArray(array));
}
scan.close();
}

public static int getMinimumIn2DArray(int[][] number) {
int minimum = number;
for (int j = 0; j < number.length; j++) {
for (int i = 0; i < number[j].length; i++) {
if (number[j][i] < minimum) {
minimum = number[j][i];
}
}
}
return minimum;
}
}``````

Output:-

Enter the number of elements in a row: 2
Enter the number of elements in a column: 2
Enter the elements for the 2d array:
15 10 20 25
2D array = [ [15, 10], [20, 25] ]
The minimum value in the given 2d array is: 10

Enter the number of elements in a row: 3
Enter the number of elements in a column: 3
Enter the elements for the 2d array:
15 484 5464 12 326 114 6565 45 9
2D array = [ [15, 484, 5464], [12, 326, 114], [6565, 45, 9] ]
The minimum value in the given 2d array is: 9

Test case where there are no elements in the array:-

Enter the number of elements in a row: 0
Enter the number of elements in a column: 0
There are no elements in the given 2d array.

In the above program to find minimum value in 2d array Java, to display the two-dimensional array we have used Arrays.deepToString() method. In Java, Arrays class contains many buit-in methods like Arrays.toString(), Arrays.sort(), Arrays.copyof(), Arrays.copyOfRange(), Arrays.fill(), Arrays.equals(), and more to solve array & matrix related problems.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!