University of Washington, AP/CS A

Lab 2: ArrayLists

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Today's lab

Goals for this lab:

Exercise : ArrayList Mystery (10.1)

Consider the following method.

public static void mystery(List<Integer> list) {
    for (int index = 0; index < list.size(); index++) {
        int elementValue = list.remove(index);
        if (elementValue % 2 == 0) {
            list.add(index);
        }
    }
    System.out.println(list);
}

Fill in the boxes with the output produced by the method when passed each of the following ArrayLists.

[5, 2, 5, 2]
[2, 2]
[^0-9,]+
[3, 5, 8, 9, 2]
[5, 9, 1, 3]
[^0-9,]+
[0, 1, 4, 3, 1, 3]
[1, 3, 3, 1]
[^0-9,]+

Exercise : ArrayList Mystery (10.1)

Consider the following method.

public static void mystery(List<Integer> list) {
    for (int i = 0; i < list.size(); i++) {
        int n = list.get(i);
        if (n % 10 == 0) {
            list.remove(i);
            list.add(n);
        }
    }
    System.out.println(list);
}

Fill in the boxes with the output produced by the method when passed each of the following ArrayLists.

[1, 20, 3, 40]
[1, 3, 20, 40]
[^0-9,]+
[80, 3, 40, 20, 7]
[3, 20, 7, 40, 80]
[^0-9,]+
[40, 20, 60, 1, 80, 30]
[20, 1, 30, 60, 40, 80]
[^0-9,]+

Exercise : ArrayList Mystery (10.1)

Consider the following method.

public static void mystery(List<Integer> list) {
    for (int i = 1; i < list.size(); i += 2) {
        if (list.get(i - 1) >= list.get(i)) {
            list.remove(i);
            list.add(0, 0);
        }
    }
    System.out.println(list);
}

Fill in the boxes with the output produced by the method when passed each of the following ArrayLists.

[10, 20, 10, 5]
[0, 10, 20, 10]
[^0-9,]+
[8, 2, 9, 7, -1, 55]
[0, 0, 8, 9, -1, 55]
[^0-9,]+
[0, 16, 9, 1, 64, 25, 25, 14, 0]
[0, 0, 0, 0, 16, 9, 64, 25, 0]
[^0-9,]+

Exercise : ArrayList Mystery (10.1)

Consider the following method.

public static void mystery(List<Integer> list) {
    for (int i = 0; i < list.size(); i++) {
        int element = list.get(i);
        list.remove(i);
        list.add(0, element + 1);
    }
    System.out.println(list);
}

Fill in the boxes with the output produced by the method when passed each of the following ArrayLists.

[10, 20, 30]
[31, 21, 11]
[^0-9,]+
[8, 2, 9, 7, 4]
[5, 8, 10, 3, 9]
[^0-9,]+
[-1, 3, 28, 17, 9, 33]
[34, 10, 18, 29, 4, 0]
[^0-9,]+

Exercise : ArrayList Mystery (10.1)

Consider the following method.

public static void mystery(List<Integer> list) {
    for (int i = list.size() - 2; i > 0; i--) {
        int a = list.get(i);
        int b = list.get(i + 1);
        list.set(i, a + b);
    }
    System.out.println(list);
}

Fill in the boxes with the output produced by the method when passed each of the following ArrayLists.

[72, 20]
[72, 20]
[^0-9,]+
[1, 2, 3, 4, 5, 6]
[1, 20, 18, 15, 11, 6]
[^0-9,]+
[10, 20, 30, 40]
[10, 90, 70, 40]
[^0-9,]+