Sort 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;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        // list.add(1);
        // list.add(6);
        // list.add(8);
        list.add(5);
        list.add(1);
        list.add(7);
        list.add(3);
        average(list);
        System.out.println(list);
    }

    public static void average(ArrayList<Integer> list){
        // int initial = list.get(0);
        int store = 0;
        for (int i = 0; i < list.size(); i++){
            for (int j = i + 1; j < list.size(); j++){
                if (list.get(i) > list.get(j)){
                    // initial = list.get(j);
                    store = list.get(i);
                    // list.remove(i);
                    list.set(i, list.get(j));
                    list.set(j, store);
                    // break;
                }
            }
        }
    }
}

<— Return Home