如何在String [] [](数组的数组)中存储String [](数组)

时间:2016-10-05 21:12:58

标签: java arrays storing-data

鉴于以下代码在

处给出空指针异常
public class Testing {
    public static final String HD_DATA = "STOCK,D1,D2,D3,D4,D5,D6,D7,D8,D9,D10";
    public static final String HD_DATA_1 = "1,10,20,30,40,50,60,70,80,90,100";
    public static final String HD_DATA_2 = "2,11,22,33,44,55,66,77,88,99,111";
    public static void main(String[] args) {
        String[] hdDataArray = HD_DATA.split(",");
        String[] hdDataArray1 = HD_DATA_1.split(",");
        String[] hdDataArray2 = HD_DATA_2.split(",");
        String[][] pram = new String[3][];   //[null, null, null]

        for(int i=0;i<3;i++) {
            for(int j=0;j<hdDataArray.length;j++) {
                if(i ==0) {
                    pram[i][j] = hdDataArray[j]; // Exception in thread "main" java.lang.NullPointerException
                }
                else if (i ==1){
                    pram[i][j] = hdDataArray1[j];
                } else if(i == 2) {
                    pram[i][j] = hdDataArray2[j];
                }
            }
        }   
        System.out.println(pram);
    }
}

尝试给出时会产生空指针异常,但是我想在运行时插入数据是否有任何方法可以在运行时插入数组数组中的数据来识别像jagged数组一样的数组数组

3 个答案:

答案 0 :(得分:4)

当您通过function calcPaddingTop() { // your logic here } jQuery(document).ready(calcPaddingTop); jQuery(window).resize(calcPaddingTop); 访问时,pram = [null, null, null]以及pram[i](数组数组中位于i位置的数组)的位置为null

pram[i][j]

答案 1 :(得分:2)

您可以通过以下几种方式执行此操作:

pram[0] = hdDataArray;
pram[1] = hdDataArray1;
pram[2] = hdDataArray2;

或者您可以直接初始化pram

String[][] pram = {
    hdDataArray,
    hdDataArray1,
    hdDataArray2
};

答案 2 :(得分:-1)

String[][] pram = new String[][] { hdDataArray, hdDataArray1, hdDataArray2 };

试试这个。您可以直接将数组分配到新数组中。