2016-04-29 123 views
-2

實際上,我想要設置arraylist的指針來索引我要提供的內容,然後我的程序會檢查是否還有元素,然後它會從我提供的那個索引中刪除所有元素。從arraylist中刪除元素

public void removefromindex(int index) 
{ 
    for (int j = notes.size(); j >notes.size(); j++) 
    { 
     notes.remove(j); 
    } 
} 

public void deleteAll(){ 
    for (Iterator<Note> it = notes.iterator(); it.hasNext();) 
    { 
     Note note = it.next(); 
     it.remove(); 
    } 
} 
+0

'爲(INT J = notes.size(); J>時notes.size() ; j ++)'< - 再次閱讀,這是沒有道理的。 – Tunaki

回答

2

您正在給一個索引,然後甚至不使用它,只是循環訪問ArrayList並刪除一些元素。你所尋找的是:

public void removefromindex(int index) 
{ 
    notes.remove(index); 
} 

要刪除所有的人,你可以簡單地使用:

public void deleteAll() 
{ 
    notes.clear(); 
} 
+0

從方法名稱判斷,我認爲他試圖從給定的索引中刪除所有項目。在這種情況下,一個簡單的解決方案是使用迭代器機制,並使用'notes.listIterator(index)'而不是默認的。 – n247s

+1

@ n247s你可能是對的,這個問題有點難以理解。 – James