Salve volevo sapere cosa sbaglio in questa implementazione:
Codice PHP:ArrayList<Integer> nodi = new ArrayList<Integer>();
// inserimento primo arco
for (int i=0; i<G.lunghezza; i++){
nodi.add(i);
}
for(int j=0;j<nodi.size();j++){
System.out.println(nodi.get(j));
}
System.out.println(nodi.size());
while(nodi.size()>0){
int head = (int)(Math.random()*nodi.size());
System.out.println("Indice nodo head = "+head);
int tail = (int)(Math.random()*nodi.size());
System.out.println("Indice nodo tail = "+tail);
if(tail!=head || nodi.size()>2){
System.out.println();
System.out.println(nodi.get(head));
System.out.println(nodi.get(tail));
nodi.remove(head);
nodi.remove(tail);
System.out.println(nodi.size());
}
int head1 = (int)(Math.random()*nodi.size());
System.out.println("Indice nodo head = "+head1);
int tail1 = (int)(Math.random()*nodi.size());
System.out.println("Indice nodo tail = "+tail1);
if(tail1!=head1 || tail1!=tail || head1!=head || nodi.size()>2){
System.out.println();
System.out.println(nodi.get(head1));
System.out.println(nodi.get(tail1));
nodi.remove(head1);
nodi.remove(tail1);
System.out.println(nodi.size());
}
nodi.size();
}
mi da questo errore:
Grazie mille!!!Codice PHP:Numero nodi = 10 Numero archi = 35
[0,0]:
[1,1]:
[2,2]:
[3,3]:
[4,4]:
[5,5]:
[6,6]:
[7,7]:
[8,8]:
[9,9]:
[10,10]:
0
1
2
3
4
5
6
7
8
9
10
11
Indice nodo head = 10
Indice nodo tail = 2
10
2
9
Indice nodo head = 7
Indice nodo tail = 5
8
6
7
Indice nodo head = 3
Indice nodo tail = 0
4
0
5
Indice nodo head = 0
Indice nodo tail = 3
1
7
3
Indice nodo head = 0
Indice nodo tail = 2
3
7
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size:
2
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.remove(ArrayList.java:387)
at Generatore3.main(Generatore3.java:65)
Press any key to continue...

Rispondi quotando