Java ArrayList of Arrays?

后端 未结 7 860
情书的邮戳
情书的邮戳 2020-12-05 01:43

I want to create a mutli dimensional array without a fixed size.

I need to be able to add items of String[2] to it.

I have tried looking at:

7条回答
  •  失恋的感觉
    2020-12-05 02:20

    1. Create the ArrayList like ArrayList action.

      In JDK 1.5 or higher use ArrayList reference name.

      In JDK 1.4 or lower use ArrayList reference name.

    2. Specify the access specifiers:

      • public, can be accessed anywhere
      • private, accessed within the class
      • protected, accessed within the class and different package subclasses
    3. Then specify the reference it will be assigned in

      action = new ArrayList();
      
    4. In JVM new keyword will allocate memory in runtime for the object.

      You should not assigned the value where declared, because you are asking without fixed size.

    5. Finally you can be use the add() method in ArrayList. Use like

      action.add(new string[how much you need])
      

      It will allocate the specific memory area in heap.

提交回复
热议问题