logo Practice-It logo

ArrayListMystery

Language/Type: Java ArrayList Collections mystery
Author: Allison Obourn (on 2012/08/11)

Consider the following method:

public static void mystery(ArrayList<Integer> list) {
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i - 1) < list.get(i)) {
            list.remove(i);
            list.add(i, list.get(i - 1));
        }
    }
}

Write the contents of the given list at the end of the call when passed each of the following ArrayLists:

[1, 2, 3, 4]
[5, -1, 3, 2, -2, 4, 2, 1]
[5, 5, 5, 0, 5, 5, 5]
[4, 3, 2, 1, 0, 0, -16, -1]

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.