数组的the aggregate initialization和dynamic aggregate initialization

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

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值