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 :: Java Implement SAM with anonymous classes in java 
Java :: how to write no in java 
Java :: how to convert a jsonobject to a dbobject 
Java :: cannot find symbol final TextView textView = root.findViewById(R.id.text_home); 
Java :: java instanciar objeto File 
Java :: integer to roman 
Java :: custom class level annotation in spring 
Java :: OkHttp3 Never Timeout on slow internet 
Java :: linux mint switch language 
Java :: autorest generate java client 
Java :: List of tuple to map + jpa 
Java :: java producer consumer queue 
Java :: java no name array eg 
Java :: how to search element in sorted array using java 
Java :: jsonobject add key value java 
Java :: JSP Convertir Int a String 
Java :: prevent creating instance of singleton from thread 
Java :: Join Two Java Strings 
Java :: class BuildConfig is public, should be declared in a file named BuildConfig.java 
Java :: jks not found when trying googlenethttptransport 
Java :: exception handling and reprompting 
Java :: android java update image dynamically 
Java :: hasAuthority method not working with thymeleaf 
Java :: java window always on top 
Java :: logger output to console twice java 
Java :: how to reorder numbers in a list randomly in java 
Java :: float division by zero 
Java :: Description Resource Path Location Type ApplicationContext cannot be resolved to a type Mobile.java /InversionOfControl/src/com/deloitte/springioc line 13 Java Problem 
Java :: error attribute fabattached not found 
Java :: how to configure multiple database in spring boot based for uat and dev environment 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =