ArrayIndexOutOfBoundsException

mbiricus Messages postés 4 Date d'inscription samedi 10 janvier 2009 Statut Membre Dernière intervention 5 août 2009 - 3 août 2009 à 18:27
kirua12 Messages postés 1155 Date d'inscription samedi 17 janvier 2004 Statut Membre Dernière intervention 29 avril 2011 - 5 août 2009 à 14:22
bonjour
j'ai developpé un programme pour inverser une matrice
mais lors de l'execution j'ai le message suivant affiché
qq'un qui peut m'aider?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Matrice.setValue(Matrice.java:27)
at Matrice.getNewMatrice(Matrice.java:89)
at Matrice.getDeterminant(Matrice.java:63)
at Matrice.getMatriceInverse(Matrice.java:102)
at Matrice.main(Matrice.java:191)
voila mon code
public class Matrice{

private float[][] coeff;

//constructor//

public Matrice(int i,int j){
this.setLength(i,j);

}

public Matrice(){
this.setLength(0,0);

}

public Matrice(float[][]mat){
this.coeff=mat;
}

//Setter//

public void setMatrice(float[][] mat){
this.coeff=mat;
}
public void setValue(int i,int j,float value){
this.coeff[i][j]=value;
}
public void setLength(int i,int j){
coeff=new float[i][j];
}
//Getter//

public float[][] getMatrice(){
return this.coeff;
}

public int getRow(){
return this.coeff.length;
}


public int getColumns(){
return this.coeff[0].length;
}

public float getValue(int i,int j){
return this.coeff[i][j];
}

//return le determinant d'une matrice//

public float getDeterminant(){

Matrice a=null;
long value=0;

if(this.getRow()<3 && this.getColumns()<3)
return this.getValue(0,0)*this.getValue(1,1)-this.getValue(0,1)*this.getValue(1,0);


for(int j=0;j<this.getColumns();j++){
a=getNewMatrice(0,j);
value +=Math.pow(-1,j)*(a.getDeterminant()*this.getValue(0,j));
}
return value;
}


public Matrice getNewMatrice(int row,int columns){

Matrice a= new Matrice(this.getRow()-1,this.getColumns()-1);
int k=-1,m=0;
for(int i=0;i<this.getRow();i++){
k++;
if(i==row){
k--;
continue;
}

m=-1;
for(int j=0;j<this.getColumns();j++){
m++;
if(i==columns){
m--;
continue;
}
float v=this.getValue(i,j);
a.setValue(k,m,v);
}


}

return a;
}


public Matrice getMatriceInverse(){
Matrice a=new Matrice(this.getRow(),this.getColumns());
Matrice temp=null;
float det=this.getDeterminant();

for(int i=0;i<this.getRow();i++){

for(int j=0;j<this.getColumns();j++){

temp=getNewMatrice(i,j);
a.setValue(i,j,(int)Math.pow(-1,i+j)*(temp.getDeterminant()/det));

}
}

return a.getMatriceTranspose();

}


public Matrice getMatriceTranspose(){

Matrice a=new Matrice(this.getRow(),this.getColumns());

for(int i=0;i<this.getRow();i++){

for(int j=0;j<this.getColumns();j++){

a.setValue(i, j,this.getValue(j,i));


}



}

return a;
}

public Matrice multiply(final Matrice matrice){

Matrice a=new Matrice(this.getRow(),this.getColumns());
long value=0;
for (int k=0; k<this.getColumns(); k++)
{
for (int i=0;i<this.getRow();i++){

for(int j=0;j<this.getRow();j++){
value +=this.getValue(j,k)*matrice.getValue(i,j);
a.setValue(i,k,value);
}


}
}

return a;
}


public String toString(){

String out="";


for(int i=0;i<this.getRow();i++){

for(int j=0;j<this.getColumns();j++)

out +=this.coeff[i][j]+"\t";

out +="\n";
}


return out;


}


public boolean isInversible(){
return (this.getDeterminant()!=0);
}


public static void main(String[] arg){

Matrice a=new Matrice(3,3);
Matrice b=new Matrice(3,3);
a.coeff=new float[][]{{2,1,0},{2,-1,1},{0,2,1}};
b=a.getMatriceInverse();


System.out.println(a.toString());
System.out.println("Matrice inverse : \n");
System.out.println(b.toString());



}


et merci

3 réponses

kirua12 Messages postés 1155 Date d'inscription samedi 17 janvier 2004 Statut Membre Dernière intervention 29 avril 2011 7
5 août 2009 à 09:32
Salut,

explication de l'erreur : tu accèdes à l'indice 2 alors que ton tableau a moins de 3 éléments.

Mets un point d'arrêt dans la méthode setValue ou getNewMatrice pour voir pourquoi ton tableau n'a pas la bonne dimension.
0
mbiricus Messages postés 4 Date d'inscription samedi 10 janvier 2009 Statut Membre Dernière intervention 5 août 2009
5 août 2009 à 12:19
bonjour
merci pour la reponse
vu que je suis debutant en java,est ce que vous pouvez m'expliquer d'avantage la solution que vous m'avez proposé.
merci
0
kirua12 Messages postés 1155 Date d'inscription samedi 17 janvier 2004 Statut Membre Dernière intervention 29 avril 2011 7
5 août 2009 à 14:22
un point d'arrêt permet d'interrompre le programme et de visualiser les différentes variables.
Regarde la doc de ton éditeur de développement (Eclipse, netbean ...) pour savoir comment en mettre un.
0
Rejoignez-nous