1

I do have a LinkedList with integer numbers 1-9

I would like to remove a specific number - say 7 - but can't be sure if it still has the index 6.

So I can't use LinkedList.remove(int index) - since the index might have change - but I also don't know how to cast the value into an Object so that I can use LinkedList.remove(Object o)

(I tried with toString() but surprisingly that didn't worked :-)

public class Main {

public static void main(String[] args) {

    LinkedList<Integer> listContent = 
                  new LinkedList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9));

    LinkedList<Integer> candidateList = new LinkedList<Integer>(listContent);
    int value = 7;
    String valueString = Integer.toString(value);
    candidateList.remove(valueString);
    System.out.println(candidateList);
    candidateList.remove(value);
    System.out.println(candidateList);
}
}

my output here is:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

[1, 2, 3, 4, 5, 6, 7, 9]
RubioRic
  • 2,442
  • 4
  • 28
  • 35

1 Answers1

2

Wrap the value in an object

candidateList.remove(new Integer(value));
Reimeus
  • 158,255
  • 15
  • 216
  • 276