LargestInArrayList.java.html

  1  import java.util.ArrayList;
  2  import java.util.Scanner;
  3  
  4  /**
  5     This program reads a sequence of values and prints them, marking the largest value.
  6  */
  7  public class LargestInArrayList
  8  {
  9     public static void main(String[] args)
 10     {  
 11        ArrayList<Double> values = new ArrayList<Double>();
 12  
 13        // Read inputs
 14  
 15        System.out.println("Please enter values, Q to quit:");
 16        Scanner in = new Scanner(System.in);
 17        while (in.hasNextDouble())
 18        {  
 19           values.add(in.nextDouble());
 20        }
 21  
 22        // Find the largest value
 23  
 24        double largest = values.get(0);
 25        for (int i = 1; i < values.size(); i++)
 26        {
 27           if (values.get(i) > largest)
 28           {
 29              largest = values.get(i);
 30           }
 31        }
 32  
 33        // Print all values, marking the largest
 34  
 35        for (double element : values)
 36        {  
 37           System.out.print(element);
 38           if (element == largest) 
 39           {
 40              System.out.print(" <== largest value");
 41           }
 42           System.out.println();
 43        }
 44     }
 45  }