Maximum Element in ArrayList

less than 1 minute read

The code has been modified slightly and now uploaded to the site.

Question

<— Return Home

Main.java

import java.util.ArrayList;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        System.out.print("Enter a sequence of numbers ending with 0 (Please type 0 to stop): ");
        boolean loop = true;
        while (loop){
            int value = input.nextInt();
            if (value == 0){
                loop = false;
            }
            else
                arrayList.add(value);
        }
        // arrayList.add(501);
        // arrayList.add(5);
        // arrayList.add(100);
        // arrayList.add(20);
        // arrayList.add(40);
        // arrayList.add(500);
        int max = max(arrayList);
        System.out.println("The biggest number is: " + max);
    }

    public static Integer max(ArrayList<Integer> arrayList){
        if (arrayList.size() == 0){
            return null;
        }
        int temp = arrayList.get(0);
        for (int i = 0; i < arrayList.size(); i++){
            if (arrayList.get(i) > temp){
                temp = arrayList.get(i);
            }
        }
        return temp;
    }
}

<— Return Home