Search code examples
javalistdeque

List of deque of integers java


this is my SSCCE

http://pastebin.com/rABzKgAx

http://pastebin.com/SypQxApe

Im trying to work with datastructure List of Deque of Integer.

So i add few integers to the deque, then add it to the finalList, but since i manipulate further with Deque and remove elements it also removes these elements from the finalList. So how am i supposed to save these items to the list ? Becuase for example, i add 30 elements to the finalList, but since i am removing the original item from Deque i simply have 30 empty elements in finalList:/. You can read the rest from the code, and on the line 57 there is a output on stdout, ant it returns empty string, so there you can see the problem.

So the problem is that i copy only reference to the object, not the object, so how am i supposed to copy object ?


Solution

  • This is where using a debugger is helpful.

    You are using

    finalList.add(tokens);
    

    and assuming you have added a copy of the list, but you are only adding a copy of the reference to the list.

    So

                tokens.add(token);
                Func(places, index+1, tokens);
                tokens.removeLast();
    

    removes every element it adds, and the list is empty.

    It appears what you intended was

            finalList.add(new LinkedList<Integer>(tokens));