1

This is not something which I would do in real life, but say:

LinkedList = a,b,c,d,e and I get their corresponding index.

Say, I want to remove b (index=1) and d (index=3)(i.e. values surrounding c (index=j=2))

Now,I do (which works fine):

When j=2
LS.remove(j + 1); ----> j=3 (d removed)
LS.remove(j - 1); ----> j=1 (b removed)

And b and d are removed.

But if, I do (does not work):

When j=2
LS.remove(j - 1); ----> j=1 (b removed)
LS.remove(j); ----> j=2 (d is not removed) (used j because due to above removal, LL has adjusted it self)

i.e. when I move the value preceding 'c' first, 'd' is not removed and the LL stays as it is. I guess, I am doing the same thing.

Am I missing out on something here?

UPDATE:

So, when I change the signature public void operation(String operator, Integer j) to public void operation(String operator, int j), it worked.

zengr
  • 38,346
  • 37
  • 130
  • 192

3 Answers3

5

If j is of type big Integer, then LinkedList.remove(Object) will be called instead of LinkedList.remove(int). And that's not what you actually want.

I don't see any reason to use big-Integer as the type of j in your second example, you should use primitive int.

Please check Why aren't Java Collections remove methods generic? on why LinkedList still has remove(Object) signature.

Community
  • 1
  • 1
denis.solonenko
  • 11,645
  • 2
  • 28
  • 23
  • See the wrong code here (try to debug if possible): Correct: http://ideone.com/jPgt9 Wrong: http://ideone.com/7RPg9 Line 53 is not removing the value. – zengr Feb 28 '11 at 03:16
  • @zengr It is because `LinkedList.remove(Object)` gets called. Interesting enough is that `remove` still has `public boolean remove(Object o)` singature, not `public boolean remove(E o)` thus allowing compiler to compile such code without errors.. – denis.solonenko Feb 28 '11 at 03:20
  • yup, got that, updated the question. The gotcha was Integer class vs int! – zengr Feb 28 '11 at 03:24
  • more over, why does remove() has Object o? any reasoning for that? – zengr Feb 28 '11 at 03:26
  • @denis.solonenko: My +1. Somehow i missed on that. With `j` as `int` it works fine. – Favonius Feb 28 '11 at 03:27
  • @zendr It is answered here http://stackoverflow.com/questions/104799/why-arent-java-collections-remove-methods-generic – denis.solonenko Feb 28 '11 at 03:35
2

It's kind of a hairy thing to refer to elements of a list by index when you're modifying the list -- and so the relation from index to element is changing. This is why, in Java, java.util.List has a method List.listIterator(), giving a java.util.ListIterator. With this, you could write a method like so:

void removeAdjacent(List<?> list, Object o) {
    ListIterator<?> listIt = list.listIterator();
    while (listIt.hasNext()) {
        if (listIt.next().equals(o)) {
            // set the iterator back to the element we just checked
            listIt.previous();
            // remove previous if it exists
            // and set the iterator again to this element
            if (listIt.hasPrevious()) {
                listIt.previous();
                listIt.remove();
                listIt.next();
            }
            // remove next if it exists
            if (listIt.hasNext()) {
                listIt.next();
                listIt.remove();
            }
        }
    }
}
rlibby
  • 5,931
  • 20
  • 25
  • I understand, that's why I said, i would not do this in real code I am writing, but I am curious that why isn't it working? – zengr Feb 28 '11 at 03:19
1

You're missing something. This complete program produces the expected result:

import java.util.LinkedList;
public class Foo {
   public static void main(String []argv) {
      LinkedList<String> l = new LinkedList<String>();
      l.add("a");
      l.add("b");
      l.add("c");
      l.add("d");
      l.add("e");
      int j = 2;
      l.remove(j - 1);
      l.remove(j);
      System.out.println(l.toString());
   }
}

The result is:

[a, c, e]
Raph Levien
  • 5,088
  • 25
  • 24
  • See the wrong code here (try to debug if possible): Correct: http://ideone.com/jPgt9 Wrong: http://ideone.com/7RPg9 Line 53 is not removing the value. – zengr Feb 28 '11 at 03:16