I have a very small program:
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
queue.add("one");
queue.add("two");
queue.add("tree");
printQueue(queue);
customizeQueue(queue);
printQueue(queue);
}
private static void customizeQueue(Queue<String> queue) {
queue.add("four");
queue.add("five");
printQueue(queue);
}
private static void printQueue(Queue<String> queue) {
for(String s: queue){
System.out.print(s + " ");
}
System.out.println();
}
I'm expecting an output of:
one two tree
one two tree four five
one two tree
However I'm getting:
one two tree
one two tree four five
one two tree four five
I'm not sure why this is happening. Am I passing the reference of the LinkedList instance? Can somebody please clarify why I'm not getting my expected output.