我只是按照我的老师的偏好使用 acm 包。
这个程序假设分配10000个有理对象,使它们成为垃圾,然后计算使用垃圾收集器之前和之后的空闲内存。然后,它应该打印垃圾收集器已清除的内存量。
import acm.program.*;
public class RuntimeGarbage extends ConsoleProgram {
public void run() {
println("Allocating 10000 Rational Objects");
for(int i=1; i==10000; i++) {
new Rational();
}
Runtime myRuntime = Runtime.getRuntime();
long free_before = myRuntime.freeMemory();
myRuntime.gc();
long free_after = myRuntime.freeMemory();
println("Garbage collection freed" + ((free_after)-(free_before)) + "bytes");
}
}
问题在于,当我尝试编译代码时,cmd 显示以下内容:
:8: error: cannot find symbol
new Rational();
with an arrow right below the R.
问题是否出在大括号内对象的创建上?
最佳答案
编译器所说的是它不知道 Rational 类型是在哪里定义的。 是的,您可以在for循环的代码块中创建对象。
根据谷歌的说法,Rational 类型没有在 acm 包中定义
rational site:www-cs-faculty.stanford.edu/~eroberts/jtf/javadoc/student/acm/
所以它一定是在其他地方定义的。
它看起来也不属于内置的java类型 http://docs.oracle.com/javase/7/docs/api/
关于Java错误: cannot find symbol Rational(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20857819/