I'm a COMPLETE beginner at coding. I was looking for the solution to this issue on this forum already but didn't manage to find it.
Right now I am stuck with coding the method removeFirstNote().
Everytime I try to compile I get an error message saying:
java.util.-ConcurrentModificationException
Here's what I've got so far... It needs to be done with a FOR-EACH-loop (school task).
public class Notebook
{
private ArrayList<String> notes;
public Notebook()
{
notes = new ArrayList<String>();
}
public void removeFirstNote(String certainString)
{ int noteNumber = 0;
boolean removed = false;
for (String note : notes){
if(note.contains(certainString) && !removed){
notes.remove(noteNumber);
removed = true;
} else {
noteNumber++;
}
}
}
You are facing ConcurrentModificationException
because you are doing two operations on the same list
at a time. ie looping and removing same time.
Inorder to avoid this situation use Iterator,which guarantees you to remove the element from list safely .
Iterator<String> it = notes.iterator();
while (it.hasNext()) {
if (condition) {
it.remove();
break;
}
}
If without iterator,
1)you need to use another list
2)Add the all elements to it.
3)Loop on original list
3)when condition met, remove
from the original list
Just break
after your first removal,
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++;
}
}
}
You would be curious to know why ConcurrentModificationException
.
For this you should have a idea how for-each
loop works.
for-each
loop of List will be internally converted to for loop with iterator.
for (Iterator<String> iterator = mylist.iterator(); iterator.hasNext();)
when you use this and then remove an element from the List, the constructed Iterator
will not know anything about that change and there will be a ConcurrentModificationException
.
you can boil this down to the following:
public void removeFirstNote(String certainString) {
for (String note: notes) {
if (note.contains(certainString)) {
notes.remove(note);
break; // exit this loop.
}
}
}
this is more efficient - no point iterating once you have found what you were looking for.
hope this helps.
Vinny Fleetwood
Try:
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
}
You can't loop over a list and remove elements at the same time. If you must use a for loop I suggest you loop through the list first and mark the item that fits and then remove it later.
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.