CSci 151: Foundations of computer science II
Home Syllabus Assignments Tests

printable version

Exam 2

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

Problem X2.1.

[8 pts] Give an invariant for the below loop that allows for the conclusion that after the loop, the ArrayList a contains no adjacent duplicates.

int i = 1;
while(i < a.size()) {
    if(a.get(i).equals(a.get(i - 1))) {
        a.remove(i);
    } else {
        i++;
    }
}