Thinking in Java 3Edition fill(boolean[ a, int from, int to, BooleanGenerator gen)( for (int i from; i< to; 1++) gennex public static void fill(byte[] a, ByteGenerator fill(a, 0, alength, gen)i public static void fill(byte[] a, int from, int to, ByteGenerator gen for (int i from; i< to; 1++) a[i]= gen next( )i public static void fill(char[] a, CharGenerator gen) t fill(a, 0, alength, gen)i public static void (int public static void fill(short[] a, ShortGe gen)( public static void fill(short[ a, int from, int to, ShortEr nerator gen) t or(in [i] gen.next public static void fill(int[ a, IntGenerator gen) fill(a, 0, alength, gen)i public static void fill(int[] a, int from, int to, IntGenerator gen) to a [i]= gen. next( public static void fill(long [ a, LongGenerator ge fill(a, 0, alength, gen)i lic static void fill(long[] a, int from, int to, Long Generator gen) 1<t public static void fill(float[] a, FloatGenerator gen) a public static void 第11页共106页 www.wgqqh.com/shhgs/tij.html
Thinking in Java 3rd Edition 第 11 页 共 106 页 www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com fill(boolean[] a, int from, int to,BooleanGenerator gen){ for(int i = from; i < to; i++) a[i] = gen.next( ); } public static void fill(byte[] a, ByteGenerator gen) { fill(a, 0, a.length, gen); } public static void fill(byte[] a, int from, int to, ByteGenerator gen) { for(int i = from; i < to; i++) a[i] = gen.next( ); } public static void fill(char[] a, CharGenerator gen) { fill(a, 0, a.length, gen); } public static void fill(char[] a, int from, int to, CharGenerator gen) { for(int i = from; i < to; i++) a[i] = gen.next( ); } public static void fill(short[] a, ShortGenerator gen) { fill(a, 0, a.length, gen); } public static void fill(short[] a, int from, int to, ShortGenerator gen) { for(int i = from; i < to; i++) a[i] = gen.next( ); } public static void fill(int[] a, IntGenerator gen) { fill(a, 0, a.length, gen); } public static void fill(int[] a, int from, int to, IntGenerator gen) { for(int i = from; i < to; i++) a[i] = gen.next( ); } public static void fill(long[] a, LongGenerator gen) { fill(a, 0, a.length, gen); } public static void fill(long[] a, int from, int to, LongGenerator gen) { for(int i = from; i < to; i++) a[i] = gen.next( ); } public static void fill(float[] a, FloatGenerator gen) { fill(a, 0, a.length, gen); } public static void
Chapter 11: Collections of Objects fill(float[] a, int from, int to, FloatGenerator gen) t for (int i m; i< to; 1++) public static void fill(double[]a tor gen)t fill(a, 0, alength, gen)i public static void fill(double[] a, int from, int to, DoubleGenerator gen)( for (int i from; i< to; 1++) a [i]= gen next()i private static Random r new Random( public static class RandBooleanGenerator implements BooleanGenerator i public boolean next()return r. nextBoolean()i) publ andByte Generator implements ByteGenerator public byte next( )I return (byte)r. nextInt( )i) private static string ssource "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopgrstuvwxy z"; private static char[ src ssource tocharArray( public static cla RandcharGenerator implements CharGenerator public char next return src[r. nextInt(src. length) public static class RandStringGenerator implements Generator rivate int len private RandcharGenerator cg=new public RandstringGenerator (int length) public Object next()t char w char llen] buf [i] cg next( )i (buf)i public static class RandshortGenerator implements shortGenerator public short next()i return (short)r.nextInt()i j public static class RandIntGenerator implements IntGenerator i private int mod =10000 第12页共106页 www.wgqqh.com/shhgs/tij.html
Chapter 11: Collections of Objects 第 12 页 共 106 页 www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com fill(float[] a, int from, int to, FloatGenerator gen) { for(int i = from; i < to; i++) a[i] = gen.next( ); } public static void fill(double[] a, DoubleGenerator gen){ fill(a, 0, a.length, gen); } public static void fill(double[] a, int from, int to, DoubleGenerator gen) { for(int i = from; i < to; i++) a[i] = gen.next( ); } private static Random r = new Random( ); public static class RandBooleanGenerator implements BooleanGenerator { public boolean next( ) { return r.nextBoolean( ); } } public static class RandByteGenerator implements ByteGenerator { public byte next( ) { return (byte)r.nextInt( ); } } private static String ssource = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy z"; private static char[] src = ssource.toCharArray( ); public static class RandCharGenerator implements CharGenerator { public char next( ) { return src[r.nextInt(src.length)]; } } public static class RandStringGenerator implements Generator { private int len; private RandCharGenerator cg = new RandCharGenerator( ); public RandStringGenerator(int length) { len = length; } public Object next( ) { char[] buf = new char[len]; for(int i = 0; i < len; i++) buf[i] = cg.next( ); return new String(buf); } } public static class RandShortGenerator implements ShortGenerator { public short next( ) { return (short)r.nextInt( ); } } public static class RandIntGenerator implements IntGenerator { private int mod = 10000;
Thinking in Java 3Edition public RandIntGenerator( )( public RandIntGenerator (int modulo) mod public int next() return r. nextInt(mod)i J public static class RandLong Generator implements LongGenerator ext() ret public static clas RandEloatGenerator implements FloatGenerator public float next()i return r. nextFloat( public static clas RandDoubleGenerator implements DoubleGenerator public double next() freturn r. nextDouble( )i }///:~ 要想用生成器( generator)来填满数组,就要传一个合适的 interface 的 reference给f()方法。这个接口应该有一个能生成正确类型的对 象的next()方法(由接口如何实现来决定)。fl()方法只是简单地调用 next(),直到填满所需的范围。现在你可以通过实现合适的 interface 来创建 generator,然后用fl()来使用这个 generator. 测试的时候随机数生成器就会很有用了,所以除了用 String生成器来代 表 Object之外,我们还创建了一整套内部类来实现所有 primitive生成 器的接口。你会看到 RandString Generator用 Rand charGenerator来填充一个char的数组,然后再把这个数组转 换成 String。这个数组的大小是由构造函数的参数所决定的。 默认情况下,为了不让生成的数字太大, RandIntGenerator会对 10,000取模。但是重载的构造函数允许你选择一个更小的数值。 下面的程序在测试类库的同时还示范了该如何使用类库。 //: cll: TestArrays2 java // Test and demonstrate Arrays2 utilities import com. bruceeckel. util.*i public class TestArrays2 t public static void main(String[] args) i int size =6 // or get the size from the command line size Integer parseInt(args [o]) System. out. println("arg must be >=3") Syster t(1) boolean[ al new boolean [size 第13页共106页 www.wgqqh.com/shhgs/tij.html
Thinking in Java 3rd Edition 第 13 页 共 106 页 www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com public RandIntGenerator( ) {} public RandIntGenerator(int modulo) { mod = modulo; } public int next( ) { return r.nextInt(mod); } } public static class RandLongGenerator implements LongGenerator { public long next( ) { return r.nextLong( ); } } public static class RandFloatGenerator implements FloatGenerator { public float next( ) { return r.nextFloat( ); } } public static class RandDoubleGenerator implements DoubleGenerator { public double next( ) {return r.nextDouble( );} } } ///:~ 要想用生成器(generator)来填满数组,就要传一个合适的 interface 的 reference 给 fill( )方法。这个接口应该有一个能生成正确类型的对 象的 next( )方法(由接口如何实现来决定)。fill( )方法只是简单地调用 next( ),直到填满所需的范围。现在你可以通过实现合适的 interface 来创建 generator,然后用 fill( )来使用这个 generator。 测试的时候随机数生成器就会很有用了,所以除了用 String 生成器来代 表 Object 之外,我们还创建了一整套内部类来实现所有 primitive 生成 器的接口。你会看到 RandStringGenerator 用 RandCharGenerator 来填充一个 char 的数组,然后再把这个数组转 换成 String。这个数组的大小是由构造函数的参数所决定的。 默认情况下,为了不让生成的数字太大,RandIntGenerator 会对 10,000 取模。但是重载的构造函数允许你选择一个更小的数值。 下面的程序在测试类库的同时还示范了该如何使用类库。 //: c11:TestArrays2.java // Test and demonstrate Arrays2 utilities. import com.bruceeckel.util.*; public class TestArrays2 { public static void main(String[] args) { int size = 6; // Or get the size from the command line: if(args.length != 0) { size = Integer.parseInt(args[0]); if(size < 3) { System.out.println("arg must be >= 3"); System.exit(1); } } boolean[] a1 = new boolean[size];
Chapter 11: Collections of Objects byte [sizer char[ a3 new char [sizeli short[ a4 new short [size] long[] a6= new long [size]i float[ a7= new float [size double[] a8 new double [sizeli Arrays2. fill(al,new Arrays2. RandBooleanGenerator())i System. out. println("a1="+ Arrays2. tostring(a1))i Arrays2. fill(a2,new Arrays2. RandByteGenerator())i System. out. println(" a2="+ Arrays2. tostring(a2))i Arrays2. fill(a3,new Arrays2. RandcharGenerator( ) System. out. println("a3 Arrays2. tostring(a3))i Arrays2. fill(a4,new Arrays2. RandshortGenerator( System. out. printin(ags) Arrays2. tostring(a4))i Arrays2. fill(a5,new Arrays2. RandIntGenerator( )) t println("a5 Arrays2. tostring(a5)) Arrays2. RandLong Generator( ))i Arrays2. tostring(a6))i Arrays2. fill(a7,new Arrays2. RandEloatGenerator( ))i System. out. println(a7="+ Arrays2. tostring (a7)) Arrays2. fill(a8,new Arrays2. RandDoubleGenerator( ) System. out. println(a8=+ Arrays2. tostring (a8)) }///: size参数有一个默认的值,不过你也可以通过命令行来设定 填满一个数组 Java标准类库 Arrays也包括了一个fi()方法,但是它太简单了;它 只是简单的把一个的值复制到数组各个位置,如果是对象,则将相同的 reference拷贝到每个位置。可以用 Arrays2 tostring()把 Arrays. fil()的工作方式清清楚楚地显示出来: //: c11: FillingArrays java Using Arrays. fill( import com. bruceeckel. simpletest* 第14页共106页 www.wgqqh.com/shhgs/tij.html emailshhgsasohu.com
Chapter 11: Collections of Objects 第 14 页 共 106 页 www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com byte[] a2 = new byte[size]; char[] a3 = new char[size]; short[] a4 = new short[size]; int[] a5 = new int[size]; long[] a6 = new long[size]; float[] a7 = new float[size]; double[] a8 = new double[size]; Arrays2.fill(a1, new Arrays2.RandBooleanGenerator( )); System.out.println("a1 = " + Arrays2.toString(a1)); Arrays2.fill(a2, new Arrays2.RandByteGenerator( )); System.out.println("a2 = " + Arrays2.toString(a2)); Arrays2.fill(a3, new Arrays2.RandCharGenerator( )); System.out.println("a3 = " + Arrays2.toString(a3)); Arrays2.fill(a4, new Arrays2.RandShortGenerator( )); System.out.println("a4 = " + Arrays2.toString(a4)); Arrays2.fill(a5, new Arrays2.RandIntGenerator( )); System.out.println("a5 = " + Arrays2.toString(a5)); Arrays2.fill(a6, new Arrays2.RandLongGenerator( )); System.out.println("a6 = " + Arrays2.toString(a6)); Arrays2.fill(a7, new Arrays2.RandFloatGenerator( )); System.out.println("a7 = " + Arrays2.toString(a7)); Arrays2.fill(a8, new Arrays2.RandDoubleGenerator( )); System.out.println("a8 = " + Arrays2.toString(a8)); } } ///:~ size 参数有一个默认的值,不过你也可以通过命令行来设定。 填满一个数组 Java 标准类库 Arrays 也包括了一个 fill( )方法,但是它太简单了;它 只是简单的把一个的值复制到数组各个位置,如果是对象,则将相同的 reference 拷贝到每个位置。可以用 Arrays2.toString( )把 Arrays.fill( )的工作方式清清楚楚地显示出来: //: c11:FillingArrays.java // Using Arrays.fill( ) import com.bruceeckel.simpletest.*;
Thinking in Java 3Edition import com. bruceeckel.util.*i import java.util.*i oublic class FillingArrays t private static Test monitor new Test( )i public static void main(String[] args)t int size =61 or get the size from the command line: nt(args [0]) boolean[ al ew boolean [sizeli e sizer char [] a3 new char [sizeli short[] a4 new short [sizeli int[ a5 =new int [size long[] a6= new long [size]i float[ a7= new float [size] double[ a8 =new double [sizeli String[] a9 new String [size]i Arrays. fill(al, true)i System. out. printin("al=+ Arrays2. tostring(a1))i Arrays. fill(a2,(byte)11)i System. out. println( a2=+ Arrays2. tostring(a2)) Arrays. fill(a3,x) System. out. println( a3=+ Arrays2. tostring(a3)) Arrays. fill(a4,(short)17)i System. out. println("a4="+ . ng Arrays. fill(a System. out. println("a5 Arrays2. tostring(a5))i Arrays. fill(a6, 23) System. out. println("a6 Arrays. tostring(a6))i Arrays. fill(a7, 29)i System. out. println (" a7=+ Arrays2. tostring(a7)) Arrays. fill(a8,47)i a Arrays2. tostring(a8)) Arrays. fill(a9,"Hello") System. out. println ("a9="+ Arrays. asList(a9))i / Manipulating ranges: rrays fill(a9,3, 5,"World") System. out. p array nitor String[] t 1,11,11] xI "a4=[17,17,17,17,17,17 [29.0,29.0,29.0,29.0,29.0,29.0 8=[47.0,47 7 47.0] Hello, Hello, Hello, Hello, Hell 第15页共106页 wgqqh. com/shhgs/tij. html
Thinking in Java 3rd Edition 第 15 页 共 106 页 www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com import com.bruceeckel.util.*; import java.util.*; public class FillingArrays { private static Test monitor = new Test( ); public static void main(String[] args) { int size = 6; // Or get the size from the command line: if(args.length != 0) size = Integer.parseInt(args[0]); boolean[] a1 = new boolean[size]; byte[] a2 = new byte[size]; char[] a3 = new char[size]; short[] a4 = new short[size]; int[] a5 = new int[size]; long[] a6 = new long[size]; float[] a7 = new float[size]; double[] a8 = new double[size]; String[] a9 = new String[size]; Arrays.fill(a1, true); System.out.println("a1 = " + Arrays2.toString(a1)); Arrays.fill(a2, (byte)11); System.out.println("a2 = " + Arrays2.toString(a2)); Arrays.fill(a3, 'x'); System.out.println("a3 = " + Arrays2.toString(a3)); Arrays.fill(a4, (short)17); System.out.println("a4 = " + Arrays2.toString(a4)); Arrays.fill(a5, 19); System.out.println("a5 = " + Arrays2.toString(a5)); Arrays.fill(a6, 23); System.out.println("a6 = " + Arrays2.toString(a6)); Arrays.fill(a7, 29); System.out.println("a7 = " + Arrays2.toString(a7)); Arrays.fill(a8, 47); System.out.println("a8 = " + Arrays2.toString(a8)); Arrays.fill(a9, "Hello"); System.out.println("a9 = " + Arrays.asList(a9)); // Manipulating ranges: Arrays.fill(a9, 3, 5, "World"); System.out.println("a9 = " + Arrays.asList(a9)); monitor.expect(new String[] { "a1 = [true, true, true, true, true, true]", "a2 = [11, 11, 11, 11, 11, 11]", "a3 = [x, x, x, x, x, x]", "a4 = [17, 17, 17, 17, 17, 17]", "a5 = [19, 19, 19, 19, 19, 19]", "a6 = [23, 23, 23, 23, 23, 23]", "a7 = [29.0, 29.0, 29.0, 29.0, 29.0, 29.0]", "a8 = [47.0, 47.0, 47.0, 47.0, 47.0, 47.0]", "a9 = [Hello, Hello, Hello, Hello, Hello, Hello]