Java dynamic array sizes?

前端 未结 18 2506
星月不相逢
星月不相逢 2020-11-22 04:37

I have a class - xClass, that I want to load into an array of xClass so I the declaration:

xClass mysclass[] = new xClass[10];
myclass[0] = new xClass();
my         


        
18条回答
  •  滥情空心
    2020-11-22 05:40

    You set the number of elements to anything you want at the time you create it:

    xClass[] mysclass = new xClass[n];
    

    Then you can initialize the elements in a loop. I am guessing that this is what you need.

    If you need to add or remove elements to the array after you create it, then you would have to use an ArrayList.

提交回复
热议问题