1

我只根据导师的喜好使用 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. 

问题可能是在大括号内创建对象吗?

4

1 回答 1

0

编译器的意思是它不知道 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/

于 2013-12-31T13:57:55.667 回答