我正在學習構造函數,並且我大部分都理解它們,但是我一定不能理解它。我也完全不明白this
。但是,下面這段代碼是應該使用這些構造函數:瞭解構造函數和「this」
默認的構造函數,這將被用來填補矩陣隨機雙打
構造函數需要一個文件對象,指向包含一個文件 矩陣,
構造函數需要一個字符串,其中包含文件
構造這需要型矩陣的值的名稱,並使得它的一個副本
構造這需要一個二維數組,並複製其值
而更多一些,具有靜態乘法方法沿。我應該使用在main
中找到的命令。但是我不太明白如何使用String作爲唯一的參數來完成它被告知的構造函數,而且其他的如默認構造函數使用隨機雙擊來填充數組。我想我應該在我的代碼中使用更多this
,但我不太確定。
我主要只需要能夠設置一個矩陣,用隨機雙精度填充m1矩陣,再用m2矩陣再做一次,然後用靜態乘法來乘它們,然後輸出結果矩陣。謝謝。 (我只是一個3×3的矩陣,最初我應該將矩陣的大小設置爲在文本文件中找到的矩陣的大小,但我也可以指定我想要的大小,對此我。而對於亂碼對不起。這得到了所有混亂了,而我試圖找出這個東西出來,恐怕進一步改變它的。)
public class Matrix {
double A[][] = new double[3][3]
// Matrix file name
public Matrix(String name) {
this(new File(name));
}
// Matrix random fill
public Matrix() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
this.A[i][j] = (min + Math.random() * (max - min));
}
// Matrix copy
public Matrix(double[][] A) {
private double[][] arrcopy = new double[3][3];
private double[][] array = new double[3][3];
array = A;
for(int i = 0; i < 3; ++i){
for(int j = 0; j < array[i].length; ++j) {
arrcopy[i][j] = array[i][j];
}
}
}
// Set array from text file
public Matrix(File a) {
File f = a;
Scanner inputStreamOne = null;
try{
inputStreamOne = new Scanner(f);
}
catch(FileNotFoundException e){
System.out.printf("Error\n");
}
double arrayOne[][] = new double[3][3];
while(inputStreamOne.hasNextInt()) {
for(int i = 0; i < 3; ++i){
for(int j = 0; j < arrayOne[i].length; ++j){
arrayOne[i][j] = inputStreamOne.nextInt();
}
}
inputStreamOne.close();
}
}
// Gets array in file from string name
public Matrix(String a) {
String inputOne = a;
Scanner inputStreamOne = null;
try{
inputStreamOne = new Scanner(new File(inputOne));
}
catch(FileNotFoundException e){
System.out.printf("Error\n");
}
while(inputStreamOne.hasNextInt()){
for(int i = 0; i < size; ++i){
for(int j = 0; j < arrayOne[i].length; ++j){
arrayOne[i][j] = inputStreamOne.nextInt();
}
}
inputStreamOne.close();
}
}
public static multiply
public static void main(String args[]) {
Matrix m = new Matrix("matrix1.txt");
Matrix m2 = new Matrix("matrix2.txt");
Matrix r = Matrix.multiply(m, m2);
r.output(...);
}
}
你能詳細說明爲什麼你覺得你需要更多地使用'this'嗎? – IceMan 2012-03-03 06:26:51