How to insert a string into a 2D string array in Java?

后端 未结 3 1943
借酒劲吻你
借酒劲吻你 2021-01-25 16:44

I\'m new to Java.

String[][] data = new String[][];
data[0][0] = \"Hello\";

This does not work, so can anyone explain why and how to make it wo

3条回答
  •  深忆病人
    2021-01-25 17:26

    You have to specify the number of rows and columns of the array while declaring it:

    String[][] data = new String[2][3];
    

    This will initialize an array with 2 rows and 3 columns. In general:

    String[][] data = new String[rows][columns];
    

    You can also ommit the number of columns:

    String[][] data = new String[2][];
    

    but to be able to fill it, you will have to initialize each row separately:

    String[][] data = new String[2][];
    data[0] = new String[3];
    data[1] = new String[3];
    

提交回复
热议问题