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.>
[url=viewtopic.php?t=14917]Ich möchte[/url] eine Sortierfunktion erstellen, die Iterator über eine ArrayList verwendet. [url=viewtopic.php?t=14917]Ich möchte[/url] 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]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
[/code]
Das Problem ist, dass nach der ersten Iteration eine Ausnahme: ConcurrentModificationError auftaucht.>