-2
我需要找到另一种方式为这个(if else statement)
:替代,如果else语句
if(representatives.indexOf(word) == -1){
representatives.add(word);
}
else if(repeatWords.indexOf(word) == -1){
repeatWords.add(word); }
我需要找到另一种方式为这个(if else statement)
:替代,如果else语句
if(representatives.indexOf(word) == -1){
representatives.add(word);
}
else if(repeatWords.indexOf(word) == -1){
repeatWords.add(word); }
与尝试含有作为DEVID解释。 contains
是checkout对象包含与否的列表函数。所以你必须修改如下代码:
if(!representatives.contains(word)){
representatives.add(word);
}
else if(!repeatWords.contains(word)){
repeatWords.add(word); }
它检查列表是否不包含对象,然后添加它。除此以外;一切似乎都好!
它的声音确定。有两种条件管理不同的列表。它们之间没有任何相似之处。 – iMBMT
你可以使用'contains'而不是'indexOf',否则,这就好了。 –
@bmt是否有比使用indexOf(word)更简单的方法? – rose