
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Find Minimum Elements in Each Row of Matrix in Java?
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.
As per the problem statement we have to find the minimum element present in every row of a given matrix of a multi-dimensional array.
Let’s deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose the original matrix is = { {2, 3,11, 4, 6}, {21,12,32,45,2}, {41,12,3,45, 2} }
The minimum element present in row-0 is 1
The minimum element present in row-1 is 2
The minimum element present in row-2 is 2
Instance-2
Suppose the original matrix is = { {2, 3, 1, 4, 6}, {21, 12, 32, -4, 2}, {4, 12, 3, 45, 2}, {-12, -13, 6, 1, 8} }
The minimum element present in row-0 is 1
The minimum element present in row-1 is -4
The minimum element present in row-2 is 2
The minimum element present in row-3 is -13
Instance-3
Suppose the original matrix is = { {2, 3, 1}, 21,12,32}, {4,12,13} }
The minimum element present in row-0 is 1
The minimum element present in row-1 is 12
The minimum element present in row-2 is 4
Algorithm
Step 1 − Declare and initialize an integer multi-dimensional array.
Step 2 − Take a loop to iterate the rows of the given matrix.
Step 3 − Inside that loop take another for loop to iterate the columns, by this method we can now check through all the available elements in rows.
Step 4 − We pass those row’s elements in a condition to get the minimum value.
Step 5 − After getting the minimum value in each loop, we print those values as output with its respective row index numbers.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length
Below refers to the syntax of it-
array.length
where, ‘array’ refers to the array reference.
Multiple Approaches:
We have provided the solution in different approaches.
By Using Static Initialization of Array Elements
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array Elements
In this approach, array elements will be initialized in the program. Then as per the algorithm the minimum values for each row of the matrix will be printed as output.
Example
public class Main{ public static void main(String[] args) { //declare an integer type multi dimentional array //here we take a 3X3 Matrix with some random values int[][] inputMatrix = { {21,34,76}, {2,1,5}, {90,23,-23} }; for (int r = 0; r < inputMatrix.length; r++) { int minimumValue = inputMatrix[r][0]; for (int c = 0; c < inputMatrix[r].length; c++){ //if the current index value is smaller than the Minimum value if (inputMatrix[r][c] < minimumValue) { //store the minimum value to the variable each time //when the above condition satisfied minimumValue = inputMatrix[r][c]; } } //print the row index value along with the derived minimum value System.out.println("The minimum element present in row-" + r + " is " + minimumValue); } } }
Output
The minimum element present in row-0 is 21 The minimum element present in row-1 is 1 The minimum element present in row-2 is -23
Approach-2: By Using User Defined Method
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm the minimum values for each row of the matrix will be printed as output.
Example
public class Main { public static void main(String[] args) { //declare an integer type multi dimentional array //a 3X3 Matrix with some random values int[][] inputMatrix ={ {2, 3, 1}, {21,12,32}, {4,12,3} }; //call the user-defined method Min(inputMatrix); } //user defined method public static void Min(int[][] mat) { for (int r = 0; r < mat.length; r++) { int minimumValue = mat[r][0]; //initiate the loop to check through the columns for (int c = 0; c < mat[r].length; c++){ //if the current index value is smaller than the Minimum value if (mat[r][c] < minimumValue) { //store the minimum value to the variable each time //when the above condition satisfied minimumValue = mat[r][c]; } } //print the row index value along with the derived minimum value System.out.println("The minimum element present in row-" + r + " is " + minimumValue); } } }
Output
The minimum element present in row-0 is 1 The minimum element present in row-1 is 12 The minimum element present in row-2 is 3
In this article, we explored different approaches to find the minimum element in each row of a matrix by using Java programming language.
- Related Articles
- Maximum sum of elements from each row in the matrix in C++
- Find maximum element of each row in a matrix in C++
- How to find the row products for each row in an R matrix?
- How to find the variance of row elements of a matrix in R?
- JavaScript Program to Find maximum element of each row in a matrix
- Find the maximum element of each row in a matrix using Python
- Program to find number of elements in matrix follows row column criteria in Python
- Program to find smallest intersecting element of each row in a matrix in Python
- How to find the row sum for each column by row name in an R matrix?
- Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python
- How to divide the matrix rows by row minimum in R?
- C++ program to find the Sum of each Row and each Column of a Matrix
- How to find the index of n number of maximums in each row of a matrix in R?
- Program to find maximum sum by flipping each row elements in Python
- Row-wise common elements in two diagonals of a square matrix in C++
