For example, in the remove (obj) function. The problem is that the number in brackets is read as an index. In this case, remove (index) is in progress.

How can this be changed?

Example:

public class L16_Test { public static void main(String[] args) { LinkedList<Integer> arr = new LinkedList<Integer>(); for (int i = 0; i < 6; i++) arr.add(i+5); out.println(arr); // output: [5, 6, 7, 8, 9, 10] arr.remove(5); out.println(arr); // output: [5, 6, 7, 8, 9] } } 

    1 answer 1

    LinkedList has two methods:

    1. remove(Object o) - removes the first element found by value.
    2. remove(int index) - removes the element at index position.

    You use the second option to use the first one to convert int to Integer:

     public class L16_Test { public static void main(String[] args) { LinkedList<Integer> arr = new LinkedList<Integer>(); for (int i = 0; i < 6; i++) arr.add(i+5); out.println(arr); arr.remove((Integer)5); // удаляет по значению out.println(arr); } } 
    • 2
      @Svetlana Popova, if this answer has solved your problem, please mark it as correct (checkmark to the left of the answer). - awesoon