Gibt es eine Möglichkeit, das ConcurrentModificificationError im folgenden Code oder eine bessere Implementierung zu übe

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Gibt es eine Möglichkeit, das ConcurrentModificificationError im folgenden Code oder eine bessere Implementierung zu übe

by Anonymous » 17 Aug 2025, 08:05

Ich möchte eine Sortierfunktion erstellen, die Iterator über eine ArrayList verwendet. Ich möchte das Array umgekehrt durchqueren und das kleinere von i-th und (i-1) -Th-Element überprüfen. Wenn i-te Element kleiner ist, bewege ich es an die Vorderseite des Arrays.

Code: Select all

import java.util.*;
// Other imports go here
// Do NOT change the class name
class Main
{
public void cq1(ArrayList list) {
ListIterator l1=list.listIterator();
ListIterator l2=list.listIterator();
while(l1.hasNext()){l1.next();l2.next();}//loop to send the listIterator to the end of ArrayList
l1.previous();//make this iterator point to the second last element
while(l1.hasPrevious())
{
int a=l1.previous();
int b=l2.previous();
if (a < b)
{
list.remove(l1.previousIndex()+1);
list.add(0, a);
}
System.out.println(list);
}

}//fn
public static void main(String[] args)
{
Main obj=new Main();
int a[]={-4, 16, 9, 1, 64, 25, 36, 4, 49};
ArrayList oe = new ArrayList();
for(int i :a)oe.add(i);
obj.cq1(oe);
}//main
}//class
Das Problem ist, dass nach der ersten Iteration eine Ausnahme: ConcurrentModificationError auftaucht.>

Top