how to get the max value of an array java
public static double arrayMax(double[] arr) {
double max = Double.NEGATIVE_INFINITY;
for(double cur: arr)
max = Math.max(max, cur);
return max;
}
how to get the max value of an array java
public static double arrayMax(double[] arr) {
double max = Double.NEGATIVE_INFINITY;
for(double cur: arr)
max = Math.max(max, cur);
return max;
}
find min in array java
private static int findMin(int[] array) {
int min = array[0];
for(int i=1;i<array.length;i++) {
if(min > array[i]) {
min = array[i];
}
}
return min;
}
java 8 find min value in array
package com.concerned.crossbill;
import java.util.Arrays;
public class Foo {
public int getMin(int[] numbers) {
return Arrays.stream(numbers).min().getAsInt();
}
}
// test class
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import com.concerned.crossbill.Foo;
public class FooTest {
public void testGetMin() {
int[] numbers = new int[]{12, 10, 31, 30, 23, 4, 5, 5, 5, 5, 10, 40};
Foo foo = new Foo();
int result = foo.getMin(numbers);
int expResult = 4;
assertEquals(expResult, result);
}
}
how to get the max value of an array java
import java.util.Random;
public class Main {
public static void main(String[] args) {
int a[] = new int [100];
Random rnd = new Random ();
for (int i = 0; i< a.length; i++) {
a[i] = rnd.nextInt(99-0)+0;
System.out.println(a[i]);
}
int max = 0;
for (int i = 0; i < a.length; i++) {
a[i] = max;
for (int j = i+1; j<a.length; j++) {
if (a[j] > max) {
max = a[j];
}
}
}
System.out.println("Max element: " + max);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us