java method to get the largest number from the int array

  • 2020-06-12 09:04:24
  • OfStack

The first thing is to have the concept of array, know what is an array, simply speaking is to store 1 group of data 1 combination, is an array... Ha ha

Given an int array, the program gets the maximum number from the array.

Thinking analysis:

1, 1 array can have a lot of Numbers, so if you want to take the largest number, that's just a number and a number and you can compare them and you can pick them, right
So: first define a variable to be the first number of the array, and then compare it with the second and third number of the array, when it encounters a larger number than itself, assign the dozen value to the variable, and then traverse to the end.

Code:


package com.itheima;
/**
 * 2 ,   known 1 a int An array of ,  Program to get the largest number from an array .
 * @author 281167413@qq.com
 */
public class Test2 {
	public static void main(String[] args)
	{
		int values[] = {1,2,9,3,4,5};
		
		int max = getMax(values);
		
		System.out.printf("The MAX values: %d\n", max);
	}
	public static int getMax(int[] values)
	{
		int tmp=Integer.MIN_VALUE;
		
		if(null!=values)
		{
			tmp=values[0];
			for(int i=0; i<values.length; i++)
			{
				if(tmp<values[i])
				{
					tmp = values[i];
				}
			}
		}
		
		return tmp;		
	}
}

This is how java gets the largest number from the int array, as shown in 1.


Related articles: