Java 是否从ArrayList中删除使用certainString找到的第一个元素?
我是一个完全的编码初学者。 我已经在这个论坛上寻找这个问题的解决方案,但没有找到 现在我一直在编写removeFirstNote()方法 每次尝试编译时,我都会收到一条错误消息,上面说: java.util.-ConcurrentModificationException 这是我到目前为止得到的。。。 它需要用FOR-EACH循环(学校任务)完成Java 是否从ArrayList中删除使用certainString找到的第一个元素?,java,arraylist,element,remove-if,Java,Arraylist,Element,Remove If,我是一个完全的编码初学者。 我已经在这个论坛上寻找这个问题的解决方案,但没有找到 现在我一直在编写removeFirstNote()方法 每次尝试编译时,我都会收到一条错误消息,上面说: java.util.-ConcurrentModificationException 这是我到目前为止得到的。。。 它需要用FOR-EACH循环(学校任务)完成 公共课堂笔记本 { 私人数组列表注释; 公用记事本( { notes=新的ArrayList(); } public void removeFirst
公共课堂笔记本
{
私人数组列表注释;
公用记事本(
{
notes=新的ArrayList();
}
public void removeFirstNote(字符串certainString)
{int noteNumber=0;
布尔值=假;
用于(字符串注释:注释){
if(note.contains(certainString)&&!已删除){
注释。删除(注释编号);
删除=真;
}否则{
noteNumber++;
}
}
}
您面临的是ConcurrentModificationException
,因为您一次在同一列表上执行两个操作,即循环和删除
为了避免这种情况,请使用迭代器,它可以保证安全地从列表中删除元素
Iterator<String> it = notes.iterator();
while (it.hasNext()) {
if (condition) {
it.remove();
break;
}
}
Iterator it=notes.Iterator();
while(it.hasNext()){
如果(条件){
it.remove();
打破
}
}
如果没有迭代器
1) 您需要使用另一个列表
2) 将所有元素添加到其中
3) 在原始列表上循环
3) 当条件满足时,从原始列表中删除
仅在第一次删除后中断
public void removeFirstNote(String certainString)
{
int noteNumber = 0;
//boolean removed = false; //No need
for (String note : notes)
{
if(note.contains(certainString))
{
notes.remove(noteNumber);
//removed = true;
break;
}
else
{
noteNumber++;
}
}
}
您可能很想知道为什么ConcurrentModificationException
为此,您应该了解每个循环的工作原理。
对于列表的每个
循环,将使用迭代器在内部转换为for循环
for (Iterator<String> iterator = mylist.iterator(); iterator.hasNext();)
for(Iterator Iterator=mylist.Iterator();Iterator.hasNext();)
当您使用此选项,然后从列表中删除一个元素时,构造的迭代器将不知道该更改的任何信息,并且将出现ConcurrentModificationException
您可以将其归结为以下内容:
public void removeFirstNote(String certainString) {
for (String note: notes) {
if (note.contains(certainString)) {
notes.remove(note);
break; // exit this loop.
}
}
}
这是更有效的-一旦你找到了你想要的东西,迭代就没有意义了
希望这有帮助
维尼·弗利特伍德试试:
for(String note : notes){
if(!notes.contains(certainString){
continue;
}
if(notes.contains(certainString)== true){
System.out.println("String: " + certainString + " has been removed");
notes.remove(certainString);
break;
}
else{
// do this
}
不能在列表上循环并同时删除元素。
如果您必须使用for循环,我建议您首先循环列表,标记适合的项,然后删除它
public void removeFirstNote(String certainString) {
int noteNumber = 0;
boolean removed = false;
int index = -1;
for (String note : notes) {
if (note.contains(certainString) && !removed) {
index = noteNumber;
removed = true;
//if your teacher allows you to, you can break the loop here and it all becomes easier
} else {
noteNumber++;
}
}
if (index != -1) {
notes.remove(index);
}
}
public void removeFirstNote(String certainString) {
int noteNumber = 0;
boolean removed = false;
int index = -1;
for (String note : notes) {
if (note.contains(certainString) && !removed) {
index = noteNumber;
removed = true;
//if your teacher allows you to, you can break the loop here and it all becomes easier
} else {
noteNumber++;
}
}
if (index != -1) {
notes.remove(index);
}