我需要更改类中的方法:
public void removeFile(int index) {
if(index >= 0 && index < files.size()) {
files.remove(index);
}
}
使用此方法:
public boolean validIndex(int index) {
if (index < 0 || index > files.size() - 1) {
return false;
} else {
return true;
}
}
代替第一种方法的这部分:
if(index >= 0 && index < files.size())
目的是第一个方法在删除文件之前检查第二个方法是否返回 true。
最佳答案
这是您要找的吗?:
public void removeFile(int index)
{
if(validIndex(index))
{
files.remove(index);
}
}
public boolean validIndex(int index)
{
return (index >= 0 && index < files.size() - 1);
}
关于java - 检查 if 语句的另一个方法的结果的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40308337/