queue27.java

来自「JAVA编程思想第四版英文原版习题答案. pdf原版的」· Java 代码 · 共 34 行

JAVA
34
字号
// holding/Queue27.java
// TIJ4 Chapter Holding, Exercise 27, page 424
/* Write a class called Command that contains a String and has a method operation()
* that displays the String. Write a second class with a method that fills a Queue
* with Command objects and returns it. Pass the filled Queue to a method in a third
* class that consumes the objects in the Queue and calls their operation() methods.
*/
import java.util.*;

class Command {
	String s;
	Command(String s) { this.s = s; }
	void operation() { System.out.print(s); }
}

class Build {	
	Queue<Command> makeQ() {
		Queue<Command> q = new LinkedList<Command>();
		for(int i = 0; i < 10; i++)
			q.offer(new Command(i + " "));
		return q;
	}
}

public class Queue27 {
	public static void commandEater(Queue<Command> qc) {
		while(qc.peek() != null)
			qc.poll().operation();
	}
	public static void main(String[] args) {
		Build b = new Build();
		commandEater(b.makeQ());	
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?