85.0k views
1 vote
The following method is intended to remove all values from the ArrayList a that have the same value as val; however, this method does not work correctly.public void removeValue(ArrayList a, int val) {int i;for (i = 0; i < a.size(); i++) {if (a.get(i) == val) {a.remove(i);}}}If the a contains 2 3 4 3 3 4 4 5 4 3 2 1 and val is equal to 4, then a should contain 2 3 3 3 5 3 2 1 after removeValue is invoked. What does a actually contain after removeValue is invoked? (2 points)1) 2 3 3 4 4 5 4 3 2 12) 2 3 3 3 4 5 3 2 13) 2 4 3 4 5 4 2 14) 2 3 3 3 5 3 2 15) 2 4 3 4 4 5 3 2 1

1 Answer

6 votes

Answer:

ArrayList a contains [2, 3, 3, 3, 4, 5, 3, 2, 1]

Explanation:

Given

The removeValue method

Required

The content of ArrayList a, after the method is called

From the question, we understand that arraylist a is:


a= [2, 3, 4, 3, 3, 4, 4, 5, 4, 3, 2, 1]


val =4

The function fails when the value to be removed appear consecutively in the list.

4 appears in index 5 and 6 of the list. Only one of the 4's will be removed

So, the updated list will be: [2, 3, 3, 3, 4, 5, 3, 2, 1]

User Ralf Stuckert
by
7.0k points