我有一个arraylist<interface>
对象被添加到for循环中的列表中.每次调用方法时,我都希望这个arraylist为空.
这是代码:
我想在这里清空的数组是建议的Phrases.
public List<Interface> returnSuggestedList(String prefix) {
String tempPrefix = prefix;
// suggestedPhrases = null;
//suggestedPhrases = new ArrayList<Interface>();
//Vector<String> list = new Vector<String>();
//List<Interface> interfaceList = new ArrayList<Interface>();
Collections.sort(wordsList);
System.out.println("Sorted Vector contains : " + wordsList);
int i = 0;
//List<String> selected = new ArrayList<String>();
for(String w:wordsList){
System.out.println(w);
if(w.startsWith(prefix.toLowerCase())) { // or .contains(), depending on
//selected.add(w); // what you want exactly
Item itemInt = new Item(w);
suggestedPhrases.add(itemInt);
}
}
Run Code Online (Sandbox Code Playgroud)
NPE*_*NPE 37
如果数组在对方法的调用中持续存在(例如,如果它是类的成员),则可以调用suggestedPhrases.clear()
清除它.
在您的示例中,似乎没有任何需要suggestedPhrases
在调用之间保持持久性,因此您可以在每次调用方法时创建(并返回)新的数组列表:
public List<Interface> returnSuggestedList(String prefix) {
ArrayList<Interface> suggestedPhrases = new ArrayList<Interface>();
// populate suggestedPhrases here
return suggestedPhrases;
}
Run Code Online (Sandbox Code Playgroud)