Implémentation d'une matrice carré avec Vector
Bonjour à tous,
Comme écris dans le titre, je suis en train d'ecrire une classe de matrice en utilisant l'object Vector, dont voici les sources:
L'interface à implémenter:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public interface Matrice{
//public int[][] getMatrice(); // return a new object
//public int getLignes();
//public int getColonnes();
public void insertData(int no_ligne, int no_colonne, int donnee);
//public int getData(int no_ligne, int colonne);
public void printMatrice();
// public void printColonne(int no_colonne);
//public void printLigne(int no_ligne);
} |
L'implémentation :
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 38 39
|
import java.util.*;
public class Vecteur implements Matrice{
private Vector vect;
private int lignes;
private int colonnes;
public Vecteur(){
this.vect = new Vector();
this.lignes = 0;
this.colonnes = 0;
}
private void agrandirVect(int size){
for(int i=lignes; i <= size ; i++)
this.vect.insertElementAt(new Vector(), i);
this.lignes = size;
}
public void insertData(int no_ligne, int no_colonne, int donnee){
if(no_ligne >= lignes)
this.agrandirVect(no_ligne);
Vector tmp = (Vector)vect.elementAt(no_ligne);
tmp.insertElementAt(donnee, no_colonne);
}
public void printMatrice(){
int col;
Vector tmp;
for(int i=0; i < vect.size(); i++){
tmp =(Vector)vect.elementAt(i);
col = tmp.size();
for(int j=0; j < colonnes; j++)
System.out.print(tmp.elementAt(j)+" ");
System.out.println();
}
}
} |
Ainsi que le programme de test:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public class Test{
public static void main(String[] args){
Matrice mat = new Vecteur();
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
mat.insertData(i,j, i+j);
mat.printMatrice();
}
} |
Voila ce que j'obtiens comme erreur à l'exécution:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 > 0
at java.util.Vector.insertElementAt(Vector.java:558)
at Vecteur.insertData(Vecteur.java:24)
at Test.main(Test.java:8)
J'arrive pas à voir pour quoi j'ai cette ArrayIndexOutOfBounds ...
Vous avez une idée ?
Merci d'avance :king: