boz22
État :
Absent
|
Bonjour,
Je souhaite faire un programme sur les graphes. J'ai trois classes : Graphe, Sommet et Global. Mon graphe possède une liste de sommet et Sommet une liste de string (pour les voisins). Mon problème est que la liste string des voisins dans sommet n'est pas conservée.
Voici le code ci-dessous : d'où provient l'erreur?
package Set;
public class Graphe {
Double MatriceAdjacence[][];
int taille;
Sommet Liste[];
Graphe(int nbSommet, Sommet L[]){
MatriceAdjacence = new Double[nbSommet][nbSommet];
this.taille = nbSommet;
for(int i=0; i<nbSommet; i++)
for(int j=0; j<nbSommet; j++)
MatriceAdjacence[i][j] = new Double(-1);
this.Liste = L;
}
void saisiePoids(String Ligne, int ind){
int index1 = 0; int s = 0;
Double un = new Double(-1);
int index2 = Ligne.indexOf(" ");
for(int l=0; l<taille-1; l++){
if(index2 != -1){
MatriceAdjacence[ind][l] = Double.valueOf(Ligne.substring(index1, index2));
index1 = index2+1;
index2 = Ligne.indexOf(" ", index1+1);
}}
MatriceAdjacence[ind][taille-1] = Double.valueOf(Ligne.substring(index1));
for(int l=0; l<taille; l++){
if(MatriceAdjacence[ind][l].compareTo(un) != 0){
Liste[ind].Voisins = new String[s+1];
Liste[ind].Voisins[s] = Liste[l].getNom();
s++;
}}
}
int getTaille(){
return this.taille;
}
void setTaille(int TailleG){
this.taille = TailleG;
}
void affichageMatrice(){
String Ligne = "";
for(int i=0; i<taille; i++){
for(int j=0; j<taille; j++){
String Temp = (MatriceAdjacence[i][j]).toString();
Ligne = Ligne.concat(Temp);
Ligne = Ligne.concat(" ");
}
System.out.println(Ligne);
Ligne = "";
}
}
void affichageListeSommet(){
String nomsSommet = "";
for(int i=0; i<this.taille; i++){
nomsSommet = nomsSommet + this.Liste[i].getNom();
nomsSommet = nomsSommet + "/";
}
System.out.println(nomsSommet);
}
}
/*******************************************************************/
public class Sommet {
String nom;
int nbVoisins;
String Voisins[];
Sommet(String n){
this.nom = n;
this.Voisins = null;
}
void setNom(String n){
this.nom = n;
}
String getNom(){
return this.nom;
}
int nbVoisins(){
return this.nbVoisins;
}
String voisinsSommet(){
String v = "";
for(int i=0; i<this.nbVoisins(); i++){
v = v + this.Voisins[i];
v = v + " ";
}
return v;
}
}
/**********************************************************************/
MAIN
package Set;
import java.lang.String;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.*;
public class Global {
static int NbSommets;
static DataInputStream DIS = new DataInputStream(System.in);
static Graphe lectureFichier(String fichier) throws IOException{
String ligne = ""; int index = 0;
BufferedReader in = new BufferedReader(new FileReader(fichier));
ligne = in.readLine();
int nbG = ligne.length();
nbG = (nbG/2) + 1;
Graphe g = new Graphe(nbG, init(ligne, nbG));
while((ligne = in.readLine()) != null){
g.saisiePoids(ligne, index);
index ++;
}
return g;
}
static Sommet[] init(String firstLigne, int longueur){
int index = 0;
String temp = null;
Sommet listeSommet[] = new Sommet[longueur];
for(int i = 0; i < longueur; i++){
temp = firstLigne.substring(index, index+1);
listeSommet[i] = new Sommet(temp);
index = index + 2;
}
return listeSommet;
}
public static void main(String[] args) {
try{
Graphe g = lectureFichier("test.txt");
g.affichageListeSommet();
g.affichageMatrice();
}
catch(IOException ioe){
System.out.println(ioe.toString());
System.exit(1);
}
}
}
|