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.
本文深入探讨了Java中数组的各种初始化方式,包括简单的数组声明、聚集初始化及动态聚集初始化等,并通过示例代码详细展示了每种初始化方法的具体用法。
1086

被折叠的 条评论
为什么被折叠?



