会不会是bruceeckel的代码有问题呢.我刚才把他的Collections2.java拿出来看. //: com:bruceeckel:util:Collections2.java // To fill any type of container using a generator object. // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 // www.BruceEckel.com. See copyright notice in CopyRight.txt. package com.bruceeckel.util; import java.util.*;public class Collections2 { // Fill an array using a generator: public static void fill(Collection c, Generator gen, int count) { for(int i = 0; i < count; i++) c.add(gen.next()); } public static void fill(Map m, MapGenerator gen, int count) { for(int i = 0; i < count; i++) { Pair p = gen.next(); m.put(p.key, p.value); } } public static class RandStringPairGenerator implements MapGenerator { private Arrays2.RandStringGenerator gen; public RandStringPairGenerator(int len) { gen = new Arrays2.RandStringGenerator(len); } public Pair next() { return new Pair(gen.next(), gen.next()); } } // Default object so you don't have to create your own: public static RandStringPairGenerator rsp = new RandStringPairGenerator(10); public static class StringPairGenerator implements MapGenerator { private int index = -1; private String[][] d; public StringPairGenerator(String[][] data) { d = data; } public Pair next() { // Force the index to wrap: index = (index + 1) % d.length; return new Pair(d[index][0], d[index][1]); } public StringPairGenerator reset() { index = -1; return this; } } // Use a predefined dataset: public static StringPairGenerator geography = new StringPairGenerator(CountryCapitals.pairs); // Produce a sequence from a 2D array: public static class StringGenerator implements Generator{ private String[][] d; private int position; private int index = -1; public StringGenerator(String[][] data, int pos) { d = data; position = pos; } public Object next() { // Force the index to wrap: index = (index + 1) % d.length; return d[index][position]; } public StringGenerator reset() { index = -1; return this; } } // Use a predefined dataset: public static StringGenerator countries = new StringGenerator(CountryCapitals.pairs, 0); public static StringGenerator capitals = new StringGenerator(CountryCapitals.pairs, 1); } ///:~ 这个代码编译也通不过,他用了个Generator接口.这个接口是com.bruceeckel.util里定义的.但是编译器总是自动加上import sun.nio.cs.Surrogate.Generator;我手动加上com.bruceeckel.util也不行.
这句代码有问题吧。
Collections2.StringPairGenerator 是一个类的名字么?
你仔细检查一下代码,问题就是这个
//: com:bruceeckel:util:Collections2.java
// To fill any type of container using a generator object.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.util;
import java.util.*;public class Collections2 {
// Fill an array using a generator:
public static void
fill(Collection c, Generator gen, int count) {
for(int i = 0; i < count; i++)
c.add(gen.next());
}
public static void
fill(Map m, MapGenerator gen, int count) {
for(int i = 0; i < count; i++) {
Pair p = gen.next();
m.put(p.key, p.value);
}
}
public static class
RandStringPairGenerator implements MapGenerator {
private Arrays2.RandStringGenerator gen;
public RandStringPairGenerator(int len) {
gen = new Arrays2.RandStringGenerator(len);
}
public Pair next() {
return new Pair(gen.next(), gen.next());
}
}
// Default object so you don't have to create your own:
public static RandStringPairGenerator rsp =
new RandStringPairGenerator(10);
public static class
StringPairGenerator implements MapGenerator {
private int index = -1;
private String[][] d;
public StringPairGenerator(String[][] data) {
d = data;
}
public Pair next() {
// Force the index to wrap:
index = (index + 1) % d.length;
return new Pair(d[index][0], d[index][1]);
}
public StringPairGenerator reset() {
index = -1;
return this;
}
}
// Use a predefined dataset:
public static StringPairGenerator geography =
new StringPairGenerator(CountryCapitals.pairs);
// Produce a sequence from a 2D array:
public static class StringGenerator implements Generator{
private String[][] d;
private int position;
private int index = -1;
public StringGenerator(String[][] data, int pos) {
d = data;
position = pos;
}
public Object next() {
// Force the index to wrap:
index = (index + 1) % d.length;
return d[index][position];
}
public StringGenerator reset() {
index = -1;
return this;
}
}
// Use a predefined dataset:
public static StringGenerator countries =
new StringGenerator(CountryCapitals.pairs, 0);
public static StringGenerator capitals =
new StringGenerator(CountryCapitals.pairs, 1);
} ///:~
这个代码编译也通不过,他用了个Generator接口.这个接口是com.bruceeckel.util里定义的.但是编译器总是自动加上import sun.nio.cs.Surrogate.Generator;我手动加上com.bruceeckel.util也不行.