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()
};
}
}
聚集初始化数组 ——》如:int[ ] dots = {6,4,8}; //
声明、构建、初始化 在一条语句中进行
The aggregate initialization must be used at the point of definition
动态聚集初始化数组(匿名数组) ——》 如:new int[ ]{6,4,8}; //
构建、初始化 在一条语句中进行
The next array initialization can be thought of as a "dynamic aggregate initialization."
W
ith this syntax you can create and initialize an array object anywhere.