I want to have a data structure conformed by a list of deques. So, taking advantage of the FIFO disposition of elements in a queue, I instantiated the following:
Deque<String> pathQueue = new ArrayDeque<>();
and also,
Queue<Deque<String>> myQueueOfDeques = new LinkedList<Deque<String>>();
However when I add each deque and print myQueueOfDeques
, it appears with the correct size of empty fields, e.g. when it is supposed to store three deques it shows [ [], [], [] ]
. Here is the portion of the code.
for (Element leaf; (leaf = (Element)walker.nextNode()) != null; ) {
for (Node node = leaf; node.getNodeType() == Node.ELEMENT_NODE; node = node.getParentNode())
{
pathQueue.addFirst(((Element)node).getAttribute("id"));
}
myQueueOfDeques.add(pathQueue);
pathQueue.clear();
}
System.out.println(myQueueOfDeques);
Clearly the problem is that I keep thinking it as the way data structures work in c++, however I know this is java and I'm having a hard time. I already read the documentation for different structures found here