public Cylinder deleteCylinder(String listNameIn) {
Cylinder result = null;
for (int i = 0; i < cylinderCount; i++) {
if (list[i].getLabel().equalsIgnoreCase(listNameIn)) {
for (int j = i; j < cylinderCount - 1; j++) {
list[j] = list[j + 1];
}
result = list[cylinderCount - 1];
cylinderCount--;
System.out.println("\t\"" + listNameIn + "\" deleted" + "\n");
}
else
System.out.println("\t\"" + listNameIn + "\" not found" + "\n");
}
return result;
}
如果我加载的文件包含名称为&#34的圆柱体;小例子&#34; &#34;媒体示例&#34; &#34;大例子&#34;和#34;真的很大的例子&#34;按此顺序,然后尝试删除大型示例,它将打印出来
&#34;大例子&#34;找不到
&#34;大例子&#34;找不到
&#34;大例子&#34;删除
有关如何解决这个问题的建议吗?
编辑:早期项目的一部分,我们最初使用的是ArrayList,但必须将其更改为数组
知道了,谢谢你们。这是正确的代码:
public Cylinder deleteCylinder(String listNameIn) {
Cylinder result = null;
for (int i = 0; i < cylinderCount; i++) {
if (list[i].getLabel().equalsIgnoreCase(listNameIn)) {
result = list[i];
for (int j = i; j < cylinderCount - 1; j++) {
list[j] = list[j + 1];
}
result = list[cylinderCount - 1];
cylinderCount--;
System.out.println("\t\"" + listNameIn + "\" deleted" + "\n");
return result;
}
}
System.out.println("\t\"" + listNameIn + "\" not found" + "\n");
return result;
}