Write a method removeFront that takes an integer n as a parameter and that remov
ID: 3555643 • Letter: W
Question
Write a method removeFront that takes an integer n as a parameter and that removes the first n values from a list of integers. For example, if a variable called list stores this sequence of values:
[8, 17, 9, 24, 42, 3, 8]
and the following call is made: list.removeFront(4); it should store the following after the call:
[42, 3, 8]
Notice that the first four values in the list have been removed and the other values appear in the same order as in the original list. You may assume that the parameter value passed is not negative and not larger than the size of the list inclusive. If the parameter value passed is 0, the list should be unchanged by the call.
Explanation / Answer
void removeFront(int n) {
Iterator i = this.iterator();
while (n > 0 && i.hasNext()) {
i.remove();
--n;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.