這裏是代碼:如何能讀Java控制檯輸出爲2D n * m個雙矩陣/陣列
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadContents {
public static void main(String[] args) throws IOException {
File file = new File("file1.txt");
List<float[]> list = new ArrayList<float[]>();
Scanner scanner = new Scanner(file).useDelimiter("\n");
while (scanner.hasNext()) {
String[] values = scanner.next().trim().split(" ");
float[] floats = new float[values.length];
for (int i = 0; i < values.length; i++) {
floats[i] = Float.parseFloat(values[i]);
}
list.add(floats);
}
float[][] values = new float[list.size()][];
for (int i = 0; i < list.size(); i++) {
values[i] = list.get(i);
for (int j = 0; j < values[i].length; j++) {
System.out.print(values[i][j] + " ");
}
System.out.println();
}
int row =values.length;
int col=values[0].length;
System.out.println(row);
System.out.println(col);
//******************************************************************************************************************
System.out.println();
double sum;
double avg=0;
for (int p = 0; p < col; p++){
sum=0;
for (int k = 0; k < row; k++){
sum = sum + values[k][p];
//avg=((double)sum/row);
}
avg=((double)sum/row);
System.out.print("average"+p+"=");
System.out.printf("%5.2f\n", avg);
}
它給出爲在二維矩陣文件內容輸出,並且還打印「的值的每列中的平均值[] []「控制檯屏幕上的矩陣。現在我想打印包含先前計算的所有平均值的矩陣到單個2d n * m數組中。 請指導我。
您是否以印刷格式打印爲 – 2011-05-03 09:31:08