Ich kann dieses Programm nicht aus einer Datei mit diesem Format (wo "Via" ist die italienische für "Straße „)Ich mache eine Übung der Programmierung für die Schule, kann nicht lösen eine "java.util.ConcurrentModificationException" Fehler
Via Emilia -> Via Botti
Via Castello -> Via Emilia
Via Appia -> Via Botti
Via Botti -> Via Emilia
Via Castello -> Via Amendola
Via Appia -> Via Ancona
Via Castello - Via Botti
Via Matteotti -> Via Carso
Via Ancona -> Via Matteotti
Via Carso -> Via Matteotti
und am dritten Frage fragt mich sind rekursiv welche Straßen zu finden ineinander (wie ein -> b und b-> a), aber wenn ich versuche, Bedingungen zu setzen, um die Möglichkeit doppelter Antworten zu entfernen (wie a und b sind rekursiv und b und a rekursiv), erhalte ich den Fehler "java.util.ConcurrentModificationException" , Was heißt das?
Dies ist der Haupt
public static void main(String[] args) throws Exception{
ArrayList<percorso> perc = new ArrayList<percorso>();
String line;
BufferedReader br = new BufferedReader(new FileReader("vie.txt"));
while ((line = br.readLine())!=null){
percorso p = new percorso (null,null);
String [] e = line.split("-->");
p.from = e[0];
p.to = e[1];
perc.add(p);
}
br.close();
//punto1(perc);
//punto2(perc);
punto3(perc);
}
Mit dieser Methode wird die Antwort auf die dritte Frage
public static void punto3(ArrayList<percorso> perc) throws Exception{
BufferedWriter bw=new BufferedWriter(new FileWriter("3.html"));
bw.write("<html> \n <body> \n");
bw.write("<h1> Punto 3 </h1> \n");
bw.write("<h3><i> Trovare le coppie di strade che formano un anello chiuso </h3></i> \n");
ArrayList<percorso> perc2 = new ArrayList<percorso>();
for (percorso k: perc)
perc2.add(k);
ArrayList<String[]> Arr = new ArrayList<String[]>();
for (percorso i: perc2){
for (percorso j: perc2){
if (i.from.equals(j.to) && i.to.equals(j.from)){
String [] e = {i.from,i.to};
Arr.add(e);
}
}
}
ArrayList<String[]> Arr2 = new ArrayList<String[]>();
for (String [] z: Arr){
Arr2.add(z);}
for (String [] f: Arr){
for (String [] j: Arr2){
if (f[0].equals(j[1]) && j[0].equals(f[1]))
Arr.remove(j);
}
}
for (String [] x: Arr)
bw.write("le vie "+x[0]+", "+x[1]+" formano un anello chiuso <br>");
bw.write("</body> \n </html> \n");
bw.close();
}
und die Klasse percorso wird die folgende
public class percorso {
String from;
String to;
public percorso (String from,String to){
this.from = from;
this.to = to;
}
}
Können Sie den Stacktrace hinzufügen oder sagen, in welcher Zeile Sie diese Ausnahme erhalten? –
Sie haben eine SO-Dokumentation zu diesem Problem. Check out: https://stackoverflow.com/documentation/java/90/collections/2197/removing-items-from-a-list-within-a-loop#t=201706281424260548454 – GPI
'Arr.remove (j);' <- Sie ändern den Inhalt von "Arr", während Sie darüber iterieren. Sie können das tun, aber Sie müssen eine 'Iterator'-Referenz verwenden, um dies zu tun, anstatt eine' for' oder eine verbesserte 'for'-Schleife. – JonK