偶比着<<Java编程思想(第二版)>>写了P361的代码,但是编译的时候出现了很多错误:operator < cannot be applied to int,java.awt.Dismension
cannot resolve symbol method get (int)
cannot resolve symbol method iterator()
cannot resolve symbol method listIterator(int)
...源代码如下:
import java.util.*;
import com.bruceeckel.util.*;public class ListPerformance{
private abstract static class Tester{
String name;
int size;
Tester(String name,int size){
this.name=name;
this.size=size;
}
abstract void test(List a,int reps);
}
private static Tester[] tests={
new Tester("get",300){
void test(List a,int reps){
for (int i = 0; i<reps; i++){
for (int j = 0; j<a.size(); j++)
a.get(j);
}
}
},
new Tester("iteration",300){
void test(List a,int reps){
for (int i = 0; i<reps; i++){
Iterator it=a.iterator();
while(it.hasNext())
it.next();
}
}
},
new Tester("insert",5000){
void test(List a,int reps){
int half=a.size()/2;
String s="test";
ListIterator it=a.listIterator(half);
for (int i = 0; i<size*10; i++)
it.add(s);
}
},
new Tester("remove",5000){
void test(List a,int reps){
ListIterator it =a.listIterator(3);
while(it.hasNext()){
it.next();
it.remove();
}
}
},
};
public static void test(List a,int reps){
System.out.println("Testing"+a.getClass().getName());
for (int i = 0; i<tests.length; i++){
Collections2.fill(a,Collections2.countries.reset(),tests[i].size);
System.out.print(tests[i].name);
long t1=System.currentTimeMillis();
tests[i].test(a,reps);
long t2=System.currentTimeMillis();
System.out.println(": "+(t2-t1));
}
}
public static void testArray(int reps){
System.out.println("Testing array as List");
for (int i = 0; i<2; i++){
String[] sa=new String[tests[i].size];
Arrays2.fill(sa,Collections2.countries.reset());
List a= Arrays.asList(sa);
System.out.print(tests[i].name());
long t1=System.currentTimeMillis();
tests[i].test(a,reps);
long t2=System.currentTimeMillis();
System.out.println(": "+(t2-t1));
}
}
public static void main(String[]args){
int reps=50000;
if(args.length>0)
reps=Integer.parseInt(args[0]);
System.out.println(reps+"repetitions");
testArray(reps);
test(new ArrayList(),reps);
test(new LinkedList(),reps);
test(new Vector(),reps);
}
}
cannot resolve symbol method get (int)
cannot resolve symbol method iterator()
cannot resolve symbol method listIterator(int)
...源代码如下:
import java.util.*;
import com.bruceeckel.util.*;public class ListPerformance{
private abstract static class Tester{
String name;
int size;
Tester(String name,int size){
this.name=name;
this.size=size;
}
abstract void test(List a,int reps);
}
private static Tester[] tests={
new Tester("get",300){
void test(List a,int reps){
for (int i = 0; i<reps; i++){
for (int j = 0; j<a.size(); j++)
a.get(j);
}
}
},
new Tester("iteration",300){
void test(List a,int reps){
for (int i = 0; i<reps; i++){
Iterator it=a.iterator();
while(it.hasNext())
it.next();
}
}
},
new Tester("insert",5000){
void test(List a,int reps){
int half=a.size()/2;
String s="test";
ListIterator it=a.listIterator(half);
for (int i = 0; i<size*10; i++)
it.add(s);
}
},
new Tester("remove",5000){
void test(List a,int reps){
ListIterator it =a.listIterator(3);
while(it.hasNext()){
it.next();
it.remove();
}
}
},
};
public static void test(List a,int reps){
System.out.println("Testing"+a.getClass().getName());
for (int i = 0; i<tests.length; i++){
Collections2.fill(a,Collections2.countries.reset(),tests[i].size);
System.out.print(tests[i].name);
long t1=System.currentTimeMillis();
tests[i].test(a,reps);
long t2=System.currentTimeMillis();
System.out.println(": "+(t2-t1));
}
}
public static void testArray(int reps){
System.out.println("Testing array as List");
for (int i = 0; i<2; i++){
String[] sa=new String[tests[i].size];
Arrays2.fill(sa,Collections2.countries.reset());
List a= Arrays.asList(sa);
System.out.print(tests[i].name());
long t1=System.currentTimeMillis();
tests[i].test(a,reps);
long t2=System.currentTimeMillis();
System.out.println(": "+(t2-t1));
}
}
public static void main(String[]args){
int reps=50000;
if(args.length>0)
reps=Integer.parseInt(args[0]);
System.out.println(reps+"repetitions");
testArray(reps);
test(new ArrayList(),reps);
test(new LinkedList(),reps);
test(new Vector(),reps);
}
}
解决方案 »
- 如何用Java实现如360安全卫士顶端菜单栏类似的样子?
- 请教一道面试题:写一个程序遍历hashtable
- JAVA前景怎么样?~
- Java中的接口可以进行类型转换和类型判断吗?
- 请问如何给JTable设置列头的名字?
- exception java.io.IOException is never thrown in body of corresponding try statement是怎么回事??
- 关于sr = new StringReader(xmlStr);的问题??????
- 求.java最简单程序
- help! JAVA下有类似VC++ 下的socket 类吗?请各位高手看一下!!
- java数组匹配求算法
- 这个问题好怪!请大家看看!谢谢!
- 有关JAVA操作文本的疑惑
public static void testArray(int reps){
System.out.println("Testing array as List");
for (int i = 0; i<2; i++){
String[] sa=new String[tests[i].size];
Arrays2.fill(sa,Collections2.countries.reset());
List a= Arrays.asList(sa);
System.out.print(tests[i].name);//这里改了一下,你对比一下
long t1=System.currentTimeMillis();
tests[i].test(a,reps);
long t2=System.currentTimeMillis();
System.out.println(": "+(t2-t1));
}
}