从arraylist中删除元素
问题描述:
实际上,我想要设置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();
}
}
答
您正在给一个索引,然后甚至不使用它,只是循环访问ArrayList并删除一些元素。你所寻找的是:
public void removefromindex(int index)
{
notes.remove(index);
}
要删除所有的人,你可以简单地使用:
public void deleteAll()
{
notes.clear();
}
'为(INT J = notes.size(); J>时notes.size() ; j ++)' Tunaki