Answers for "get lowest number in array list java"

1

how to get the highest value in a list java

import java.util.*;
public class CollectionsMaxExample2 {
public static void main(String[] args) {
//Create collections lists.
List<Integer> list = Arrays.asList(20, 10, 100, 140, 250);
Integer max = Collections.max(list);
System.out.println("Maximum element is: "+max);
}
Posted by: Guest on January-08-2021
0

how to find the smallest numbers in an arraylist java

import java.util.ArrayList;
 
import java.util.Scanner;
 
public class IndexOfSmallest {
 
    public static void main(String[] args) {
 
        Scanner scanner = new Scanner(System.in);
      
        ArrayList<Integer> list = new ArrayList<>();
 
        while (true) {
 
            int input = Integer.valueOf(scanner.nextLine());
 
            if (input == 9999) {
 
                break;
 
            }
 
            list.add(input);
 
        }
 
        System.out.println("");
 
        int smallest = list.get(0);
 
        int index = 0;
 
        while (index < list.size()) {
 
            if (list.get(index) < smallest) {
 
                smallest = list.get(index);
 
            }
 
            index++;
 
        }
 
        System.out.println("Smallest number: " + smallest);
 
        index = 0;
 
        while (index < list.size()) {
 
            if (list.get(index) == smallest) {
 
                System.out.println("Found at index: " + index);
 
            }
 
            index++;
 
        }
 
    }
 
}
Posted by: Guest on November-20-2020

Code answers related to "get lowest number in array list java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language