Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to find the largest number in a 2d array java

// Assume the 2D array is holding int values
// Method for finding largest number in array given below
// if 2D array is null or empty, return min int value

public int findLargest(int[][] mat) {
	int max = Integer.MIN_VALUE; // running max value
  	if(mat == null || mat.length == 0) {
     	return max; // return min int if mat is null or empty 
    }
  
  	final int ROW = mat.length; // nb of rows in 2D array
  	final int COL = mat[0].length; // nb of cols in 2D array
  	// Iterate over elements to find largest value
  	for(int row=0; row < ROW; row++) {
    	for(int col=0; col < COL; col++) {
        	if(mat[row][col] > max) {
            	max = mat[row][col]; // update running max if larger val is found 
            }
        }
    }
	
  	return max;
}
Comment

find largest number in 2d array java

System.out.println(Collections.max(Stream.of(new int[][]{{1, 2}, {3, 4}})
        .map(a -> Collections.max(Arrays.stream(a).boxed()
            .collect(Collectors.toList())))
            .collect(Collectors.toList())));
Comment

PREVIOUS NEXT
Code Example
Java :: i have AdoptOpenJDK 8 but java --version gettinbg Unrecognized option: --version Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. 
Java :: how to initialise array in java without size 
Java :: highlight selected item in recyclerview android 
Java :: get current unix timestamp java 
Java :: how to create a circle in java 
Java :: declare array with values java 
Java :: how to create an array in java 
Java :: spring boot procfile run profile 
Java :: reading csv file in java 
Java :: execute code after delay java 
Java :: len of number in java 
Java :: java template string 
Java :: java random word from list 
Java :: change button text java 
Java :: java docstring javadoc 
Java :: how to make a string lowercase in java 
Java :: jframe border 
Java :: how to convert int into int array of digits in java 
Java :: java add button 
Java :: view get text android 
Java :: convert date to offsetdatetime in java 
Java :: convert base64 to image png without saving 
Java :: from string to date 
Java :: android list[i] 
Java :: java list distinct by object attribute 
Java :: java return tuple 
Java :: java opts 
Java :: break java 
Java :: string length in java 
Java :: spring boot actuator 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =