I NEED HELP URGENTLY!
-
public static void IzbrisZivali(ArrayListSeznam) throws ConcurrentModificationException // brisanje zivali
, IOException
// IzbrisZivali je ime funkcije, (Arraylist - dinamicna tabela, Zivali - nase zivali, Seznam - uporabljala bom tole za iskanje mojega vnosa)
{BufferedReader vhod=new BufferedReader(new InputStreamReader(System.in)) ; // odpiranje bralnika { System.out.println("Vnesi točno ime živali, ki bi jo radi izbrisali: "); String IzbrisZivali=vhod.readLine(); // preberemo ime int v=0; // spremenljivka nam pomaga, ce se ne najde te zivali v nasem seznamu for(Zivali Element: Seznam) //za vsak element iz seznama if(IzbrisZivali.equals(Element.getIme())) // ce je ime iskane zivali isto kot ime trenutne zivali v nasem seznamu... { Seznam.remove(Element); // jo odstranimo iz nasega seznama zivali System.out.println("Žival " + IzbrisZivali + " je bila izbrisana"); v++; } if(v==0) // ce ne najdemo zivali, to sporocimo { System.out.println(); System.out.println("Na seznamu ni živali z imenom " + IzbrisZivali); System.out.println(); } } }
How can i fix that? What should be changed. Please solve me this problem as im sitting here and staring at my computer for 2 hours! Please! The problem is ConcurrentModificationException
Regards, Matjaž
-
public static void IzbrisZivali(ArrayListSeznam) throws ConcurrentModificationException // brisanje zivali
, IOException
// IzbrisZivali je ime funkcije, (Arraylist - dinamicna tabela, Zivali - nase zivali, Seznam - uporabljala bom tole za iskanje mojega vnosa)
{BufferedReader vhod=new BufferedReader(new InputStreamReader(System.in)) ; // odpiranje bralnika { System.out.println("Vnesi točno ime živali, ki bi jo radi izbrisali: "); String IzbrisZivali=vhod.readLine(); // preberemo ime int v=0; // spremenljivka nam pomaga, ce se ne najde te zivali v nasem seznamu for(Zivali Element: Seznam) //za vsak element iz seznama if(IzbrisZivali.equals(Element.getIme())) // ce je ime iskane zivali isto kot ime trenutne zivali v nasem seznamu... { Seznam.remove(Element); // jo odstranimo iz nasega seznama zivali System.out.println("Žival " + IzbrisZivali + " je bila izbrisana"); v++; } if(v==0) // ce ne najdemo zivali, to sporocimo { System.out.println(); System.out.println("Na seznamu ni živali z imenom " + IzbrisZivali); System.out.println(); } } }
How can i fix that? What should be changed. Please solve me this problem as im sitting here and staring at my computer for 2 hours! Please! The problem is ConcurrentModificationException
Regards, Matjaž
You can't iterate this way over your list (Seznam) while removing elements at the same time. Instead you should you use an iterator and call remove on the iterator:
Iterator iter = Seznam.iterator();
while (Seznam.hasNext()) {
Zivali element = iter.next();
...
...
if (.....) {
iter.remove();
}}
BTW: this is a forum not a chat, so you can't request "URGENT HELP!". Your question is not more urgent than other questions. If it is really urgent, then you should pay somebody to fix the problem for you. And please, use a better title next time you ask a question, everybody knows that if you ask a question, you want some help... Please, read the posting guidelines at the top of this message board.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
public static void IzbrisZivali(ArrayListSeznam) throws ConcurrentModificationException // brisanje zivali
, IOException
// IzbrisZivali je ime funkcije, (Arraylist - dinamicna tabela, Zivali - nase zivali, Seznam - uporabljala bom tole za iskanje mojega vnosa)
{BufferedReader vhod=new BufferedReader(new InputStreamReader(System.in)) ; // odpiranje bralnika { System.out.println("Vnesi točno ime živali, ki bi jo radi izbrisali: "); String IzbrisZivali=vhod.readLine(); // preberemo ime int v=0; // spremenljivka nam pomaga, ce se ne najde te zivali v nasem seznamu for(Zivali Element: Seznam) //za vsak element iz seznama if(IzbrisZivali.equals(Element.getIme())) // ce je ime iskane zivali isto kot ime trenutne zivali v nasem seznamu... { Seznam.remove(Element); // jo odstranimo iz nasega seznama zivali System.out.println("Žival " + IzbrisZivali + " je bila izbrisana"); v++; } if(v==0) // ce ne najdemo zivali, to sporocimo { System.out.println(); System.out.println("Na seznamu ni živali z imenom " + IzbrisZivali); System.out.println(); } } }
How can i fix that? What should be changed. Please solve me this problem as im sitting here and staring at my computer for 2 hours! Please! The problem is ConcurrentModificationException
Regards, Matjaž
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. That's what the documentation says. You might figure out what happens when you make your function non-static. And please debug your code. - do not name variables with capital letter ("Element") That's really dangerous and can have side effects as Object names are started with a capital letter (e.g."System", "BufferedReader"). - always use English comments. regards Torsten
I never finish anyth...