元のキューをコピーしてキューの内容を出力してから、コピーを再度実行して、キュー内の要素の総数を出力しようとしています。元のキューで CopyQueue メソッドを実行し、それを ShowQueue メソッドの入力として使用すると、元のキューが変更されます。
public static void main(String[] args) {
LinkedUnbndQueue test = new LinkedUnbndQueue();
test.enqueue('a');
test.enqueue('b');
System.out.println( showQueue(CopyQueue(test)) );
System.out.println( Count(CopyQueue(test)) );
}
public static LinkedUnbndQueue CopyQueue(LinkedUnbndQueue orig){
LinkedUnbndQueue copy = orig;
return copy;
}
public static int Count(LinkedUnbndQueue orig){
int count = 0;
while(!orig.isEmpty() ){
orig.dequeue();
count = count + 1;
}
return count;
}
public static String showQueue(LinkedUnbndQueue orig){
String str = "";
while(!orig.isEmpty()){
str = str + orig.dequeue() + " ";
}
return str;
}