How to remove every preceding and succeeding value of an instance of an object in a ListIterator

77 Views Asked by At

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.

1

There are 1 best solutions below

0
On

Try the below:

private static void removePreceeding(ListIterator listIterator, Object value) {
        while (listIterator.hasNext()) {
            if (listIterator.next().equals(value)) {
                if (listIterator.hasPrevious()) {
                    listIterator.previous();// Note that alternating calls to
                                            // next and previous will return the
                                            // same element repeatedly. Hence
                                            // call another previous to move the
                                            // cursor one level backward
                    listIterator.previous();
                    listIterator.remove();
                    listIterator.next(); // Since the cursor was moved back,
                                            // reset it to next element (i.e
                                            // current element which was visited
                                            // previously)
                }
            }
        }
    }

private static void removeSucceeding(ListIterator listIterator, Object value) {
        while (listIterator.hasNext()) {
            if (listIterator.next().equals(value)) {
                if (listIterator.hasNext()) {
                    listIterator.next();
                    listIterator.remove();
                }
            }
        }
    }