该方法不返回局部变量的值.
我可以使用以下方法中的局部变量索引的值
public boolean contains(Object input) {
int index = 0;
while(myAsetIterator.hasNext()) {
index++;
if(input.equals(myAsetIterator.next())) {
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
在此方法中作为我要删除的对象的数组的索引.
public boolean remove(Object o) {
int count = 0;
if(o == null) {
return false;
}
if(contains(o)) {
genArray[index] == null;
}
if (count > 0) {
System.out.println(count+" same elements were present in Aset. "
+ "Removed all those "+count+" elements from Aset.");
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我知道局部变量的范围仅限于它声明的方法.但是,如果不使用字段/实例变量,我可能还有一种方法可能无法实现.我不是那么擅长编程,但我绝对不知道大师们提供的所有小技巧和技巧.值得一试.谢谢你的时间.
不.它对方法来说是局部的,它只存在于该方法中.选项是:
remove以便获取索引.那会很难过:(作为最后两个的例子,你可以写:
public int indexOf(Object input) {
int index = 0;
while(myAsetIterator.hasNext()) {
index++;
if (input.equals(myAsetIterator.next())) {
return index;
}
}
return -1;
}
public boolean contains(Object input) {
return indexOf(input) == -1;
}
Run Code Online (Sandbox Code Playgroud)
...然后在你的remove方法中,你将使用indexOf而不是contains.