probleme avec des ArrayList
Bonjour
je souhaite implementer une matrice d'adjacence pour representer des graphes
pour cela j'ai decider d'utiliser des arraylist d'arraylist
mon code compile bien cependant a l'execution eclipse m'affiche plein d'erreures:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
import java.util.ArrayList;
public class matrice_adjacent {
private int nb_somet;
private ArrayList<ArrayList<Boolean>> matrice;
public matrice_adjacent(int nbre_som){
nb_somet = nbre_som;
matrice = new ArrayList<ArrayList<Boolean>>(nbre_som);
/* for (int i=0;i<=nbre_som; i++){
ArrayList<Boolean> m = new ArrayList<Boolean>(nbre_som);
matrice.add(i,m);
}*/
}
public void ajout_arete(int S1,int S2){
matrice.get(S1).add(S2,true);
}
public void suprim_arete(int S1,int S2){
matrice.get(S1).set(S2,false);
}
/**
* @param args
*/
public static void main(String[] args) {
matrice_adjacent m = new matrice_adjacent(5);
m.ajout_arete(2,2);
m.ajout_arete(3,2);
m.ajout_arete(4,2);
}
} |
et voici les erreures que j'ai a l'execution:
Code:
1 2 3 4 5 6
|
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at matrice_adjacent.ajout_arete(matrice_adjacent.java:18)
at matrice_adjacent.main(matrice_adjacent.java:40) |
si quelqu'un pouvait me dire d'ou ça vient??!!
merci!