偶比着<<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);
}
}

解决方案 »

  1.   

    赶问一句,楼主引入com.bruceeckel.util.*;这个包了??
      

  2.   

    应该是没有 com.bruceeckel.util.*;
      

  3.   

    看错误信息 应该是有什么没有import
      

  4.   

    相信是搂住的笔误,
    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));
    }
    }
      

  5.   

    这个程序是要有前面程序的支持的,,Collections2这个类在前几页的程序里肯定是个类,而且是打到import com.bruceeckel.util.*;包里的,你得把这个类也写一下编译一下