java - Can you use Math.max with an array? -
java - Can you use Math.max with an array? -
package prova1; import javax.swing.joptionpane; /** * * @author ooo */ public class prova1 { public static void main(string[] args) { int array[] = new int[10]; (int = 0; < array.length; i++) { string input = joptionpane.showinputdialog("insert number"); array[i] = integer.parseint(input); } joptionpane.showmessagedialog(null, math.max(array)); } }
can utilize math.max array?
no, but...
if you're using java 8, can utilize streams:
arrays.stream(array).max().getasint()
otherwise can write simple utility method you:
public static int max(int... array) { if (array.length == 0) { // ... } int max = array[0]; (int : array) { if (a > max) max = a; } homecoming max; }
java arrays math
Comments
Post a Comment