且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在运行时创建对象?

更新时间:2023-12-06 15:14:34

阅读有关 Java教程中的数组.

class Spam {
  public static void main(String[] args) {

    int n = Integer.valueOf(args[0]);

    // Declare an array:
    Foo[] myArray;

    // Create an array:
    myArray = new Foo[n];

    // Foo[0] through Foo[n - 1] are now references to Foo objects, initially null.

    // Populate the array:
    for (int i = 0; i < n; i++) {
      myArray[i] = new Foo();
    }

  }
}