如何从java中链接到数组的数组中删除值。jar文件?
items数组是在jar中声明的,它的大小和值由我决定。我试过这个
public void itemsDelete(int x)
{
Item[] temp=new Item[items.length-1];
for(int i=0;i<temp.length;i++)
{
if(i!=x)
{
temp[i]=items[i];
}
}
items=new Item[temp.length]
for(int i=0;i<temp.length;i++)
{
items[i]=temp[i];
}
}
和一个if(items[i]!=null){code…} 但在这两种情况下,当我运行它时,都会看到线程“AWT-EventQueue-0”java.lang.NullPointerException“”中的“异常”。可能是什么问题
# 1 楼答案
第一个循环总是在复制最后一个项目之前停止,而且索引x处的临时数组中不会复制任何值
假设items是一个字符串数组:
“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”
你想删除索引5中的项目,代码如下:
“0”,“1”,“2”,“3”,“4”,空,“6”,“7”
这可能解释了为什么会出现空指针异常,可能是在稍后处理items数组时
这可能会做更多你想做的事情,而且,正如Perception所建议的,你可以在完成任务时分配等于temp的项目