how to find the smallest numbers in an arraylist java

Solutions on MaxInterview for how to find the smallest numbers in an arraylist java by the best coders in the world

showing results for - "how to find the smallest numbers in an arraylist java"
Giovanni
20 Mar 2019
1import java.util.ArrayList;
2 
3import java.util.Scanner;
4 
5public class IndexOfSmallest {
6 
7    public static void main(String[] args) {
8 
9        Scanner scanner = new Scanner(System.in);
10      
11        ArrayList<Integer> list = new ArrayList<>();
12 
13        while (true) {
14 
15            int input = Integer.valueOf(scanner.nextLine());
16 
17            if (input == 9999) {
18 
19                break;
20 
21            }
22 
23            list.add(input);
24 
25        }
26 
27        System.out.println("");
28 
29        int smallest = list.get(0);
30 
31        int index = 0;
32 
33        while (index < list.size()) {
34 
35            if (list.get(index) < smallest) {
36 
37                smallest = list.get(index);
38 
39            }
40 
41            index++;
42 
43        }
44 
45        System.out.println("Smallest number: " + smallest);
46 
47        index = 0;
48 
49        while (index < list.size()) {
50 
51            if (list.get(index) == smallest) {
52 
53                System.out.println("Found at index: " + index);
54 
55            }
56 
57            index++;
58 
59        }
60 
61    }
62 
63}