Answers for "find min value in array java"

0

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;
	}
Posted by: Guest on May-10-2020
0

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);
  }
}
Posted by: Guest on July-02-2020
-2

finding min and max from given number in java

Scanner input = new Scanner(System.in);
        // Minimum And Maximum 
        int count = 0;
        int min = 0;
        int max = 0;
        boolean bugSolved = true;
		/* or we can use :
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        */
		
        while (true){
            int cnt = count++;
            System.out.print("Enter Number #"+(cnt+1)+": ");
            boolean isValid = input.hasNextInt();
            if(isValid){
               int num = input.nextInt();
               /* if (bugSolved){
                   bugSolved = false;
                   min = num;
                   max = num;
               }  # Just remove this condition and 
               	boolean (bugSolved) at the top, if you use 
               	int min = Integer.MAX_VALUE and int max = 
                Integer.MIN_VALUE */
                if (num < min) {
                    min = num;
                }else if (num > max){
                    max = num;
                }
            }else{
                System.out.println("Invalid input..");
                break;
            }
            input.nextLine();
        }
        System.out.println("Min Number : " + min);
        System.out.println("Max Number : " + max);
Posted by: Guest on July-02-2020

Code answers related to "find min value in array java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language