1
我想要創建一個通過ArrayList創建矩陣的類。從ArrayList創建矩陣
所以這就是我所做的:
public class Matrice implements IMatrice {
ArrayList elements;
private int numLignes;
private int numColonnes;
public static void main(String[] args) {
Matrice test = new Matrice(3, 4, 6.0);
System.out.println(test);
}
public Matrice (int numLignes, int numColonnes, double valeur){
this.numLignes = numLignes;
this.numColonnes = numColonnes;
elements = new ArrayList(numLignes * numColonnes);
for(int i = 0; i < numLignes * numColonnes; i++){
elements.add(i, valeur);
}
}
}
現在,我創造了這個,我想嘗試,如果它的工作原理。然後,我創造了這個toString()
方法:
public String toString() {
final DecimalFormat DEC_FORMAT = new DecimalFormat("0.0");
final int ESP = 8;
int num;
String sTmp;
String s = "[";
for (int i = 0 ; i < (numLignes * numColonnes) ; i++) {
//etendre i sur ESP colonnes
sTmp = "";
num = ESP - DEC_FORMAT.format(elements.get(i)).length();
for (int j = 0 ; j < num ; j++) {
sTmp = sTmp + " ";
}
sTmp = sTmp + DEC_FORMAT.format(elements.get(i));
if (i != 0 && i % numColonnes == 0) {
s = s + " ]\n[" + sTmp;
} else {
s = s + sTmp;
}
}
s = s + " ]";
return s;
}
然後,這是我的主要嘗試矩陣:
public static void main(String[] args) {
Matrice test = new Matrice(3, 4, 6.0);
System.out.println(test);
}
,我不知道爲什麼,但我只得到這樣的:
[ ]
我知道一件小事是錯的,但我找不到什麼。你可以幫幫我嗎?
你甚至可以標記您自己的答案接受;-) – harpun 2015-02-24 19:21:13
它說,我可以在2天內做... – biiwop 2015-02-24 20:38:01