I have been trying to do these methods for a couple days now. The point is, I am given two methods, one that removes the preceding value and one that removes the succeeding value.
In each, I am given a parameter of type ListIterator and another of type Object. The instance of a value that I am checking for is the Object in the ListIterator.
For example, when removing the preceding value, I am given a list such as:
[12, 42, 28, 92, 3, 25, 3, 89]
I am supposed to remove the element before every occurrence of the number 3 such that it becomes:
[12, 42, 28, 3, 3, 89]
When removing the succeeding value, I am given a list such as:
[12, 42, 28, 92, 3, 25, 3, 89]
I am supposed to remove the element after every occurrence of the number 3 such that it becomes:
[12, 42, 28, 92, 3, 3]
I have attempted to do this already, but with no success. This is my current code:
Remove preceding:
public static void removePreceeding(ListIterator it, Object value) {
if (it.hasNext()) {
it.next();
} else {
return;
}
while (it.hasNext()) {
if (it.next().equals(value)) {
it.previous();
it.remove();
}
}
}
Remove succeeding:
public static void removeSucceeding(ListIterator it, Object value) {
while (it.hasNext()) {
if (it.next().equals(value)) {
if (it.hasNext()) {
it.next();
it.remove();
}
}
}
}
All help is appreciated. Thank you.
Try the below: