-5

我尝试通过将数组放入 for 循环来从字符串数组中获取我不想要的字符串。

我尝试下面的代码,但我得到 java.lang.NullPointerException ,我不知道为什么。

感谢您的帮助。

String unWanted = new String[]{"","a","b","c"};

String[] temp = new String[unWanted.length-1];
int p = 0;
for (int k = 0; k < unWanted.length; k++){
    if(unWanted[k].equals("") == false)
    {
        temp[p] = unWanted[k];
        p++;
    }
}
4

4 回答 4

5

这里唯一的错误是String数组声明

String[] unWanted = new String[]{"","a","b","c"};
于 2013-05-10T14:09:07.450 回答
4

temp长度为 3,但您在循环中的位置 4 处插入值。

于 2013-05-10T14:07:33.953 回答
2

首先改变这一行

String unWanted = new String[]{"","a","b","c"};

String[] unWanted = new String[]{"","a","b","c"};
于 2013-05-10T14:09:37.140 回答
2

它甚至在编译吗?

String unWanted = new String[]{"","a","b","c"};

改成

 String[] unWanted = new String[]{"","a","b","c"};

更改后我的console. 它工作正常

于 2013-05-10T14:10:24.207 回答