OnJava8-Examples/arrays/ArrayOptions.java

80 lines
2.2 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// arrays/ArrayOptions.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Initialization & re-assignment of arrays
2015-06-15 17:47:35 -07:00
import java.util.*;
2016-01-25 18:05:55 -08:00
import static onjava.ArrayShow.*;
2015-06-15 17:47:35 -07:00
public class ArrayOptions {
public static void main(String[] args) {
// Arrays of objects:
2017-01-20 21:30:44 -08:00
BerylliumSphere[] a; // Uninitialized local
2015-06-15 17:47:35 -07:00
BerylliumSphere[] b = new BerylliumSphere[5];
2015-06-15 17:47:35 -07:00
// The references inside the array are
// automatically initialized to null:
2016-01-25 18:05:55 -08:00
show("b", b);
2015-06-15 17:47:35 -07:00
BerylliumSphere[] c = new BerylliumSphere[4];
for(int i = 0; i < c.length; i++)
if(c[i] == null) // Can test for null reference
c[i] = new BerylliumSphere();
2015-06-15 17:47:35 -07:00
// Aggregate initialization:
2016-01-25 18:05:55 -08:00
BerylliumSphere[] d = {
new BerylliumSphere(),
new BerylliumSphere(),
new BerylliumSphere()
2015-06-15 17:47:35 -07:00
};
2015-06-15 17:47:35 -07:00
// Dynamic aggregate initialization:
a = new BerylliumSphere[]{
new BerylliumSphere(), new BerylliumSphere(),
};
2016-01-25 18:05:55 -08:00
// (Trailing comma is optional)
2015-11-03 12:00:44 -08:00
System.out.println("a.length = " + a.length);
System.out.println("b.length = " + b.length);
System.out.println("c.length = " + c.length);
System.out.println("d.length = " + d.length);
2015-06-15 17:47:35 -07:00
a = d;
2015-11-03 12:00:44 -08:00
System.out.println("a.length = " + a.length);
2015-06-15 17:47:35 -07:00
// Arrays of primitives:
int[] e; // Null reference
int[] f = new int[5];
2015-06-15 17:47:35 -07:00
// The primitives inside the array are
// automatically initialized to zero:
2016-01-25 18:05:55 -08:00
show("f", f);
2015-06-15 17:47:35 -07:00
int[] g = new int[4];
for(int i = 0; i < g.length; i++)
g[i] = i*i;
int[] h = { 11, 47, 93 };
2015-12-18 11:28:19 -08:00
// Compile error: variable e not initialized:
2016-01-25 18:05:55 -08:00
//- System.out.println("e.length = " + e.length);
2015-11-03 12:00:44 -08:00
System.out.println("f.length = " + f.length);
System.out.println("g.length = " + g.length);
System.out.println("h.length = " + h.length);
2015-06-15 17:47:35 -07:00
e = h;
2015-11-03 12:00:44 -08:00
System.out.println("e.length = " + e.length);
2015-06-15 17:47:35 -07:00
e = new int[]{ 1, 2 };
2015-11-03 12:00:44 -08:00
System.out.println("e.length = " + e.length);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
b: [null, null, null, null, null]
a.length = 2
b.length = 5
c.length = 4
d.length = 3
a.length = 3
f: [0, 0, 0, 0, 0]
f.length = 5
g.length = 4
h.length = 3
e.length = 3
e.length = 2
2015-09-07 11:44:36 -06:00
*/