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.
If
jis of type bigInteger, thenLinkedList.remove(Object)will be called instead ofLinkedList.remove(int). And that’s not what you actually want.I don’t see any reason to use big-Integer as the type of
jin your second example, you should use primitiveint.Please check Why aren't Java Collections remove methods generic? on why
LinkedListstill hasremove(Object)signature.