Home > Tutorial > Java Find Min Max Number Array

Java Find Min Max Number Array

Java sample code that will determine both the minimum (min) and maximum (max) values based on a list of numbers contained within a Java Array. The for loop is the main processing piece keeps assigning the biggest and smallest value looking at each number in the array.


Java Find Min Max Number Array Source Code

package com.jcd.java.tutorials;

public class JavaFindMinMaxArray 
{

	public static void main(String[] args) 
	{
		
		//Define an array of integers
		int numbers[] = new int[] {21,43,12,87,45,67,36,99,54,22,76,19};
		
		//Assign minimum to the first element
		int minimum = numbers[0];
		
		//Assign the maximum to the first element
		int maximum = numbers[0];
		
		//Loop through to find the minimum and maximum
		for(int x=1; x < numbers.length; x++)
		{
			if(numbers[x] > maximum) { maximum = numbers[x]; }			
			
			else if (numbers[x] < minimum) { minimum = numbers[x]; }		
		}
		
		//Print out the maximum and minimum values
		System.out.println("Maximum Number: " + maximum);
		System.out.println("Minimum Number: " + minimum);

	}

}


Java Find Min Max Number Array Output

Maximum Number: 99
Minimum Number: 12


Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot