正在加载图片...
Thinking in Java 3Edition Initialization re-assignment of arrays import com. bruceeckel. simpletest. class Weeble [) //A small mythical creature ublic class Arraysize private static Test monitor new Test( public static void main(String[] args)f // Arrays of objects Weeble[] a; / Local uninitialized variable Weeble[ b= new Weeble[5]; // Null references Weeble[ c= new Weeble[4]i for(int i =0; i< c. length; i++) if(c[il = null)// Can test for null referenc / Aggregate initialization Weeble[ d=i new Weeble(), new Weeble(), new Weeble( // Dynamic aggregate initialization a= new Weeble[ i new Weeble(), new Weeble( System. out. println("alength="+ alength)i System. out. println("blength ="+blength)i / The references inside the array are // automatically initialized to null for(int i =0; i< blength; i++) System. out. println(" c. length =+ clength)i System. out. println("d.lengt tln("a length t a length) // Arrays of primitives: int[l e; //Null t[]f t[5] t[ g w int [41 for(int i =0 g[i] 11,47,93} // Compile error: variable e not initialized //! System. out. println("e length="+ elength) Syst t println("f length =" f length)i / The primitives inside the array are automatically initialized to zero for (int 0; i<f length; i++) System. out. println("f["+ i +]=" f[i])i ngt g length) System. out.pr System. out System. out. println("elength ="+elength) monitor. expect(new String[] b lenath "b[1]=nu11", 第3页共106页 www.wgqqh.com/shhgs/tij.htmlThinking in Java 3rd Edition 第 3 页 共 106 页 www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com // Initialization & re-assignment of arrays. import com.bruceeckel.simpletest.*; class Weeble {} // A small mythical creature public class ArraySize { private static Test monitor = new Test( ); public static void main(String[] args) { // Arrays of objects: Weeble[] a; // Local uninitialized variable Weeble[] b = new Weeble[5]; // Null references Weeble[] c = new Weeble[4]; for(int i = 0; i < c.length; i++) if(c[i] == null) // Can test for null reference c[i] = new Weeble( ); // Aggregate initialization: Weeble[] d = { new Weeble( ), new Weeble( ), new Weeble( ) }; // Dynamic aggregate initialization: a = new Weeble[] { new Weeble( ), new Weeble( ) }; System.out.println("a.length=" + a.length); System.out.println("b.length = " + b.length); // The references inside the array are // automatically initialized to null: for(int i = 0; i < b.length; i++) System.out.println("b[" + i + "]=" + b[i]); System.out.println("c.length = " + c.length); System.out.println("d.length = " + d.length); a = d; System.out.println("a.length = " + a.length); // Arrays of primitives: int[] e; // Null reference int[] f = new int[5]; int[] g = new int[4]; for(int i = 0; i < g.length; i++) g[i] = i*i; int[] h = { 11, 47, 93 }; // Compile error: variable e not initialized: //!System.out.println("e.length=" + e.length); System.out.println("f.length = " + f.length); // The primitives inside the array are // automatically initialized to zero: for(int i = 0; i < f.length; i++) System.out.println("f[" + i + "]=" + f[i]); System.out.println("g.length = " + g.length); System.out.println("h.length = " + h.length); e = h; System.out.println("e.length = " + e.length); e = new int[] { 1, 2 }; System.out.println("e.length = " + e.length); monitor.expect(new String[] { "a.length=2", "b.length = 5", "b[0]=null", "b[1]=null
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有