In: Computer Science
This is the simplest way to write this program.
import java.util.Scanner;
import java.util.ArrayList;
public class first{
public static void main(String[] args) {
//to read input
Scanner input = new
Scanner(System.in);
// Create an ArrayList
ArrayList<Integer> list = new
ArrayList<Integer>();
System.out.print("Enter a sequence
ending wih 0: ");
Integer number =
input.nextInt();
while (number.intValue() != 0)
{
list.add(number);
number =
input.nextInt();
}
// Display the largest number in
the input
System.out.println("The largest
number in the input is " + max(list));
}
//Return the maximum value in an ArrayList of
integers
public static Integer max(ArrayList<Integer>
list) {
if (list.size() == 0)
return null;
Integer max = list.get(0);
for (int i = 0; i < list.size();
i++) {
if (list.get(i)
> max)
max = list.get(i);
}
return max;
}
}