find largest number in single array java code example

Example 1: java how to find the largest number in an arraylist

List<Integer> myList = new ArrayList<>();

for(int i = 0; i < 10; i++){
    myList.add(i);
}
//gets highest number in the list
int highestNumber = Collections.max(myList);

System.out.Println(highestNumber);

//output: 9

Example 2: Java program to find largest of three numbers

java program find largest of three numbers using command line argument
public class UsingCommandLineArgument
{
   public static void main(String[] args) 
   {
      int number1 = Integer.parseInt(args[0]);
      int number2 = Integer.parseInt(args[1]);
      int number3 = Integer.parseInt(args[2]);   
      if(number1 > number2 && number1 > number3)
      {
         System.out.println(number1 + " is largest number.");
      }
      else if(number2 > number1 && number2 > number3)
      {
         System.out.println(number2 + " is largest number.");
      }
      else   
      {
         System.out.println(number3 + " is largest number.");
      }
   }
}

Tags:

Java Example