将多维数组中的第一个元素分配给一维数组



我正在尝试将二维数组文件夹数组中每个数组的第一个元素添加到一个名为tempArray的一维数组中,如下所示的代码所示。但是,我从临时数组中收到空指针异常。我该如何解决这个问题?

    int listLength = folderArray.length;
    String tempArray[] = null;
    for(int x = 0; x<listLength;x++){
        tempArray[x] = folderArray[x][0];
    }

您必须先初始化tempArray,然后才能为其元素分配任何内容:

String tempArray[] = new String[listLength];

是一个好的开始(而不是String tempArray[] = null;

因为您将tempArray[]分配为null

将其更改为,
String tempArray[] = new String[listLength];

最新更新