Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

arraylist sorter

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class arraylistSorter {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int size;
        do {
            System.out.print("Enter array size (min: 5): ");
            size = input.nextInt();
        } while (!(size >= 5));

        ArrayList<Integer> array = new ArrayList<>(size);

        System.out.println("Enter the numbers: ");
        for (int i = 0; i < size; i++) {
            array.add(input.nextInt());
        }
        System.out.println("Unsorted array: "+ array);

        Collections.sort(array);
        System.out.println("Sorted array: " + array);

        System.out.println("minimum value: ");
        getMinimum(array);

        System.out.println("maximum value: ");
        getMaximum(array);

    }
    private static void getMinimum (ArrayList<Integer> arr) {
        int min = arr.get(0);
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i) < min) {
                min = arr.get(i);
            }
        }
        System.out.println(min);
    }

    private static void getMaximum (ArrayList<Integer> arr) {
        int max = arr.get(0);
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i) > max) {
                max = arr.get(i);
            }
        }
        System.out.println(max);
    }

}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #arraylist #sorter
ADD COMMENT
Topic
Name
9+8 =