Reviver_ace
Reviver_ace

Reputation: 19

LinkedList removes wrong index

I have very strange problem and i do not know what cause it. It appears that remove method deletes wrong element from linkedList. Here is quick example of it:

LinkedList<Long> list = new LinkedList<>();
int pos=1;
System.out.println("pos+1:"+(pos+1)); //prints 2 which is true
list.remove(pos+1); //deletes 5th element
//if i write list.remove(2); it will delete 2th element which is of course ok

It happens at 4th loop (index from 0) and for this input : 8 5 1 2 3; Also i will add that for input 3 1 2 3; everything works fine.

Here I paste all the code. Thank you for help

import java.util.LinkedList;
import java.util.Scanner;

class Zad3 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        long iloscZnakow = 0;
        long wyrazCiagu;
        LinkedList<Long> list = new LinkedList<>();

        long iloscOperacji = input.nextLong();
        iloscZnakow += String.valueOf(iloscOperacji).length();
        long pos = 0;
        while (input.hasNext()) {
            wyrazCiagu = input.nextLong();
            list.add(wyrazCiagu);
            iloscZnakow += String.valueOf(wyrazCiagu).length();
        }

        long wartoscPrzesuniecia;
        for (long i = 0; i < iloscOperacji; i++) {
            System.out.print(i + ":postac poczatkowa ciagu:");
            for (long l : list) {
                System.out.print(l + " ");
            }
            System.out.println();
            System.out.println("pos aktualna pozycja:" + list.get((int) pos) + " indeks:" + pos);

            if (list.get((int) pos) % 2 == 0) {// R
                System.out.print("wykonana operacja R ");
                if (pos == list.size() - 1) {
                    wartoscPrzesuniecia = list.get(0);
                } else {
                    wartoscPrzesuniecia = list.get((int) pos + 1);
                }
                System.out.println();
                System.out.println("pos+1:" + (pos + 1));
                list.remove(pos + 1);
                System.out.println("c=" + wartoscPrzesuniecia);
                if (pos + wartoscPrzesuniecia >= list.size()) {
                    while (pos < list.size()) {
                        pos++;
                        wartoscPrzesuniecia--;
                    }
                    pos = wartoscPrzesuniecia;
                } else {
                    pos += wartoscPrzesuniecia;
                }

            } else {// X
                System.out.print("wykonana operacja X ");
                wartoscPrzesuniecia = list.get((int) pos);
                long liczba = wartoscPrzesuniecia - 1;
                list.add((int) (pos + 1), liczba);

                System.out.println("c=" + wartoscPrzesuniecia);
                if (pos + wartoscPrzesuniecia >= list.size()) {
                    while (pos < list.size()) {
                        pos++;
                        wartoscPrzesuniecia--;
                    }
                    pos = wartoscPrzesuniecia;
                } else {
                    pos += wartoscPrzesuniecia;
                }
            }
            System.out.print(":postac koncowa ciagu:");
            for (long l : list) {
                System.out.print(l + " ");
            }
            System.out.println();
            System.out.println("finalna pozycja POS: " + list.get((int) pos));
        }

        for (long i = pos; i < list.size(); i++) {
            System.out.print(list.get((int) i) + " ");
        }
        for (long i = 0; i < pos; i++) {
            System.out.print(list.get((int) i) + " ");
        }
        System.out.println();
        System.out.print(iloscZnakow);
        input.close();
    }
}

Upvotes: 0

Views: 82

Answers (1)

Shane Sepac
Shane Sepac

Reputation: 826

Your LinkedList is of type Long- and this is important in this case. You're confusing the compiler with these two methods:

1. LinkedList.remove(Object o)
2. LinkedList.remove(int index)

Now ask yourself, what does the following code do when your LinkedList has type Long?

LinkedList.remove(5);

Deciding which remove method is called depends on whether the compiler interprets the above numeric parameter ("5" in the example above) as an int or as a Long.

Try explicitly telling the compiler that you want to use the LinkedList.remove(int index) version of the method by using an explicit cast in your code:

LinkedList<Long> list = new LinkedList<>();
int pos=1;
System.out.println("pos+1:"+(pos+1));
list.remove((int)pos+1); //Add an explicit cast here to your above code!

Upvotes: 2

Related Questions