How to get largest and smallest element in 2d array in java column wise I have got output for row wise
Switch the order of the loops in order to find column wise minimum and maximum:
for (int col = 0; col < data[0].length; col++) { // assuming all row have same length
int large = data[0][col],
small = data[0][col];
for (int row = 1; row < data.length; row++) {
if (large < data[row][col]) {
large = data[row][col];
}
if (small > data[row][col]) {
small = data[row][col];
}
}
System.out.println("\nlargest values:" + large);
System.out.println("smallest values:" +small);
}
Output:
largest values:9
smallest values:-10
largest values:6
smallest values:2
largest values:5
smallest values:-100
largest values:-2
smallest values:-45
Largest Row or Column in Matrix
You have almost gotten the solution, the only problem is that you are not resetting the sum1 and sum2 before calculating the sum of each row and column respectively. You should do it right before the second loop in each case.
Java: Compare elements of 2D array(matrix) in the column And finding row number containing smallest value in that column
First of all I think you are confused about row and column of an array. Your array has 3 rows and 4 columns. I will assume you want to find the column index of smallest element of each row. If so, find below code:
double arr[][] = {{2, 3, 4, 6},
{3, 6, 7.0, 3.3},
{2.1, 3.4, 2, 7.7}
};
for (int i = 0; i < arr.length; i++) {
int column = 0;
for (int j = 1; j < arr[i].length; j++) {
column = (arr[i][column] < arr[i][j]) ? column : j;
}
System.out.println("Smallest element for row " + i + " = " + column +" th column");
}
Hope this helps ;)
Updated one(to find row number):
double arr[][] = {{2, 3, 4, 6},
{3, 6, 7.0, 3.3},
{2.1, 3.4, 2, 7.7}
};
for(int j=0;j<arr[0].length;j++)
{
int row = 0;
for (int i = 1; i < arr.length; i++) {
row = (arr[row][j] < arr[i][j]) ? row : i;
}
System.out.println("Column = " + j + " Row = " + row);
}
Jagged array; check for column-wise value increase
This approach takes a row. It considers 'this' row and the one after it. It considers N
number of columns, where N
is the minimum of the number of columns in this row and the row after. In math, if R
is the number of rows in this 2D matrix, take some r1: r1 ∈ [0, R) and r2 = r1 + 1. Then, N = min{num_cols(r1), num_cols(r2)}.
In column n
, where n ∈ [0, N]
, if the value at the column in the next row happens to be smaller than the value in the preceding row, it returns false. If everything else worked, it returns true.
public static boolean columnValuesIncrease(int[][] t) {
for(int i = 0 ; i < t.length - 1 ; i++)
for(int j = 0 ; j < Math.min(t[i].length, t[i+1].length) ; j++)
if(t[i][j] > t[i+1][j])
return false;
return true;
}
Related Topics
Calling Method That Exists in Child Classes But Not in Parent Class
Jackson @Jsonproperty Not Working If Property Name Not Equal Field Name
Javax.Validation - Allow Null But Validate If the Value Is Not
The Package Org.W3C.Dom Is Accessible from More Than One Module: <Unnamed>, Java.Xml
How to Subtract a Constant Value from All Elements of an Array
Spring Boot: Cannot Access Rest Controller on Localhost (404)
How to Configure Hikaricp in My Spring Boot App in My Application.Properties Files
How to Pass or Send Data from Recyclerview Adapter to Fragment
Mock Objects Created Inside Method Under Test to Verify the Arguments Passed
Extract Text Br Tags in Selenium Java
Passing List from Thymeleaf to Spring Controller
Creating a Composite Unique Constraints on Multiple Columns
Transactionrequiredexception Executing an Update/Delete Query
Is There a Java Setting for Disabling Certificate Validation