2016-04-29 127 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