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


i have been trying these methods couple days now. point is, given 2 methods, 1 removes preceding value , 1 removes succeeding value.

in each, given parameter of type listiterator , of type object. instance of value checking object in listiterator.

for example, when removing preceding value, given list such as:

[12, 42, 28, 92, 3, 25, 3, 89] 

i supposed remove element before every occurrence of number 3 such becomes:

[12, 42, 28, 3, 3, 89] 

when removing succeeding value, given list such as:

[12, 42, 28, 92, 3, 25, 3, 89] 

i supposed remove element after every occurrence of number 3 such becomes:

[12, 42, 28, 92, 3, 3] 

i have attempted already, no success. 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 appreciated. thank you.

try below:

private static void removepreceeding(listiterator listiterator, object value) {         while (listiterator.hasnext()) {             if (listiterator.next().equals(value)) {                 if (listiterator.hasprevious()) {                     listiterator.previous();// note alternating calls                                             // next , previous return                                             // same element repeatedly. hence                                             // call previous move                                             // cursor 1 level backward                     listiterator.previous();                     listiterator.remove();                     listiterator.next(); // since cursor moved back,                                             // reset next element (i.e                                             // current element 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();                 }             }         }     } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -