如何从链接到.jar文件的java数组中删除值?
items数组是在jar中声明的,它的大小和值由我来决定如何从链接到.jar文件的java数组中删除值?,java,arrays,graphics,jar,nullpointerexception,Java,Arrays,Graphics,Jar,Nullpointerexception,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.lengt
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];
}
}
public void items删除(int x)
{
项目[]温度=新项目[项目长度-1];
对于(int i=0;i第一个循环总是在复制最后一个项目之前停止,并且索引x处的临时数组中不会复制任何值
想象项是一个字符串数组:
“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”
您想删除索引5处的项目,代码如下:
“0”,“1”,“2”,“3”,“4”,空,“6”,“7”
这可能解释了为什么会出现空指针异常,可能是在稍后处理items数组时
这可能会做更多您想要做的事情,并且,正如Perception所建议的,您可以在完成时只分配等于temp的项目
public void itemsDelete(int x)
{
Item[] temp=new Item[items.length-1];
//This variable will keep track of the index in the temp array
int j = 0;
//for each of the items in the input array...
for(int i=0;i<items.length;i++)
{
if(i!=x)
{
temp[j]=items[i];
//We've copied a value so increment the temp index...
j++;
}
}
items = temp;
}
public void items删除(int x)
{
项目[]温度=新项目[项目长度-1];
//此变量将跟踪临时数组中的索引
int j=0;
//对于输入数组中的每个项。。。
对于(int i=0;i第一个循环总是在复制最后一个项目之前停止,并且索引x处的临时数组中不会复制任何值
想象项是一个字符串数组:
“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”
您想删除索引5处的项目,代码如下:
“0”,“1”,“2”,“3”,“4”,空,“6”,“7”
这可能解释了为什么会出现空指针异常,可能是在稍后处理items数组时
这可能会做更多您想要做的事情,并且,正如Perception所建议的,您可以在完成时只分配等于temp的项目
public void itemsDelete(int x)
{
Item[] temp=new Item[items.length-1];
//This variable will keep track of the index in the temp array
int j = 0;
//for each of the items in the input array...
for(int i=0;i<items.length;i++)
{
if(i!=x)
{
temp[j]=items[i];
//We've copied a value so increment the temp index...
j++;
}
}
items = temp;
}
public void items删除(int x)
{
项目[]温度=新项目[项目长度-1];
//此变量将跟踪临时数组中的索引
int j=0;
//对于输入数组中的每个项。。。
对于(int i=0;iYou可以只做items=temp
,您知道。列表将是用于此需求的更好的数据结构。您知道,您可以只做items=temp
。列表将是用于此需求的更好的数据结构。