Merci @thelvin ton aide a été précieuse je m'étais compliqué la vie pour rien à la fin donc en clair la façon dont je l'avais imaginé ne marchera pas ?
En plus j'ai fait ceci mais je suis bloqué au niveau de l'addition lorsque j'initialise s à Null il me dit sort une erreur NullPointerException mais si je ne l'affecte pas null à la création il refuse de compiler
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 38 39 40 41 42 43 44
| public class Matrice {
private int [][] matrice;
//constructeur par defaut
public Matrice(int [][] tab){
matrice = tab.clone();
}
//methode toString
//methode getNbLignes
public int getNbLignes(){
return matrice.length;
}
public int getNbColonnes(){
return matrice[0].length;
}
public void Affichage(){
for(int i=0; i<this.getNbLignes(); i++){
for(int j=0; j<this.getNbColonnes(); j++){
System.out.println("A l'indice [" +i+ "] et [" +j+ "] on a: "+matrice[i][j]);
}
}
}
//matrice somme
public Matrice Ajouter(Matrice autreMatrice){
Matrice somme = null;
int [][] t1;
t1 = matrice.clone();
int [][] t2;
t2=autreMatrice.matrice.clone();
int [][] s = null;
//t2 = matrice.clone();
for(int i=0;i<this.getNbLignes();i++){
for(int j=0;j<this.getNbColonnes();j++){
s[i][j] = t1[i][j] + t2[i][j];
}
}
somme.matrice = s.clone();
return somme;
}
} |
A noter aussi que ma variable matrice étant privé comment faire la somme sans faire
int [][] copie_matrice1 = this.matrice.clone();
Partager