我必须编写一个程序来比较两个不同列表的运行时间,一个是基于数组的,另一个是链接列表。我必须以二十个不同的长度运行二十次,所以我做了一个for循环来为我做这个,它应该运行二十次,但只打印一件事,然后它让我的电脑非常慢,它说这个过程还在我的IDE上运行了一段时间。继承人的代码,问你是否需要更多,但这应该是足够的。我想知道为什么for循环只打印一个东西,之后它说仍然在运行。这个for循环有什么问题,它打印什么?
public class Foo4 {
public static void main(String[] args) {
int max = Integer.MAX_VALUE;
int diff = max/20;
int holder = 1;
long test1time;
long test2time;
StopWatch timer = new StopWatch();
int t;
for (t = 0; t < 20; t++) {
AList test1 = new AList();
Slist test2 = new Slist();
for (int j = 0; j < holder; j++) {
test1.addFirst("the");
test2.addFirst("thee");
}
timer.reset();
timer.start();
test1.getLast();
timer.stop();
test1time = timer.getElapsedTime();
timer.reset();
timer.start();
test2.getLast();
timer.stop();
test2time = timer.getElapsedTime();
System.out.println(test1time + " : " + test2time);
holder += diff;
}
}
}
修复您的格式。 – MarsAtomic 2014-09-27 18:59:41
如果您花时间和精力来格式化您的代码,其他人会更乐意投入时间和精力来帮助您。你目前的格式非常糟糕,很难阅读,因为你的代码块没有缩进,其他块看起来是随机缩进的,叠加的花括号... – 2014-09-27 19:00:42
我可以得到一些不太模糊的东西吗?无论如何,我会将其格式化,这使得它更容易,但我不明白格式化的问题?新人应该如何知道如何格式化以使其他人更容易? – trosy 2014-09-27 19:00:57