如何在 Java 中初始化动态数组?

java 8object oriented programmingprogramming

以下程序演示了如何初始化先前声明的数组。

示例

public class Tester {
   int a[];
   public static void main(String[] args) {
      Tester tester = new Tester();
      tester.initialize();
   }
   private void initialize() {
      a = new int[3];
      a[0] = 0;
      a[1] = 1;
      a[2] = 2;
      for(int i=0; i< a.length ; i++) {
         System.out.print(a[i] +" ");
      }
   }
}

输出

0 1 2

相关文章