⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex11(4).java

📁 JAVA编程思想第四版英文原版习题答案. pdf原版的
💻 JAVA
字号:
// containers/Ex11.java
// TIJ4 Chapter Containers, Exercise 11, page 829
/* Create a class that contains an Integer that is initialized 
* to a value between 0 and 100 using java.util.Random. Implement
* Comparable using this Integer field. Fill a PriorityQueue with 
* objects of your class, and extract the values using poll() to 
* show that it produces the expected order.
*/
import java.util.*;

class IntegerTest implements Comparable<IntegerTest> {
	Random r = new Random();
	Integer i = r.nextInt(100);
	public int compareTo(IntegerTest arg) {
		int d = this.i - arg.i;
		return d < 0 ? -1 : d == 0 ? 0 : 1;
	}
	public String toString() {
		return Integer.toString(i);
	}
}

public class Ex11 {
	public static void main(String[] args) {
		PriorityQueue<IntegerTest> pt = 
		new PriorityQueue<IntegerTest>();
		for(int i = 0; i < 20; i++) {
			pt.add(new IntegerTest());
		}
		int size = pt.size();
		for(int i = 0; i < size; i++) {
			System.out.print(pt.poll() + " ");
		}
	}
} 

⌨️ 快捷键说明

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