java读取text存入二维数组中

大侠回答的《java读取text存入二维数组中》代码中,貌似for循环里面的转换有点小问题!
另外想请教下,如果不知道读取的text有多少行,double数组就不能实例化,这个怎么来解决啊??求指导!!

import java.io.*;
import java.util.*;

public class Demo {
    
    public static void main(String[] args)throws Exception{
        double[][] arr = getFile("D:\\xx.txt");
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i].length; j++){
                System.out.print(arr[i][j]+"  ");
            }
                System.out.println();
        }
    }
    private static double[][] getFile(String pathName)throws Exception{
        File file = new File(pathName);
        if(!file.exists())
            throw new RuntimeException("Sorry,Not File!");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String str = br.readLine();
        List<double[]> list = new ArrayList<double[]>();
        while((str=br.readLine())!=null){
            int j = 0;
            String[] arr = str.split(" +");
            double[] dArr = new double[arr.length];
            for(String ss : arr){
                //上次手动写的,没测试,这里确实有点问题
                dArr[j++] = Double.parseDouble(ss);
            }
            list.add(dArr);
        }
        int max = 0;
        for(int i = 0; i < list.size(); i++){
            if(max < list.get(i).length)
                max = list.get(i).length;
        }
        //这个是动态的了,数组长度。
        double[][] sanjiaoxing = new double[list.size()][max];
        for(int i = 0; i < sanjiaoxing.length; i++){
            for(int j = 0; j < list.get(i).length; j++){
                //这是一种写法,有点复杂。
                sanjiaoxing[i][j] = list.get(i)[j];
            }
        }
        return sanjiaoxing;
    }
}

//测试文件还是那个

sadsadsadsa
123.00   1234.34   //注意:这里少了一数,程序也可以的
123.00   1267.34  12
4545.00   6767.34  76

//测试结果:

123.0  1234.34  213.0  
123.0  1267.34  12.0  
4545.0  6767.34  76.0

//程序基本能满足需求,请自行优化。

来自:求助得到的回答
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-07-17
动态扩容数组,或者使用集合呗
double[]  ds = new double[];
if 有下一行 {
   ds = Arrays.copyOf(ds,ds.length+1);
   ds[ds.length - 1] = 读取到的数据;
}
集合比较简单  List lsit = new ArrayList();
if 有下一行
 list.add(读取到的数据);

追问

我是新手,可不可以写的详细点?

这是我写的!!但是考虑到text里的数据较多,n行3列,不知道行数?我该怎么处理??求大侠帮忙

追答

public static void main(String[] args) throws Exception {
double[][] dataread = new double[0][0];
File file = new File("d:\\array.txt");
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while((line = in.readLine()) != null){
String[] temp = line.split("\t");
//扩容行
dataread = Arrays.copyOf(dataread, dataread.length+1);
for(int i=0;i<temp.length; i++){
//得到新的一行
double[] lastrow = dataread[dataread.length-1];
//扩容列
lastrow=Arrays.copyOf(lastrow, lastrow.length+1);
lastrow[lastrow.length-1] = Double.parseDouble(temp[i]);
}
}
in.close();

for(int i=0; i<dataread.length; i++){
for(int j=0; j<dataread[i].length; j++){
System.out.println(dataread[i][j]);
}
}
}

第2个回答  2014-07-17
import java.io.*;public class FileDemo{ public static void main(String[] args)throws Exception{ double[][] arr = getFile("D:\\xx.txt"); for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } private static double[][] getFile(String pathName)throws Exception{ File file = new File(pathName); if(!file.exists()) throw new RuntimeException("Sorry,Not File!"); BufferedReader br = new BufferedReader(new FileReader(file)); String str = br.readLine(); double[][] sanjiaoxing = new double[3][3]; int i = 0; while((str=br.readLine())!=null){ int j = 0; String[] arr = str.split(" +"); for(String list : arr){ sanjiaoxing[i][j++] = Double.parseDouble(arr); } ++i; } return sanjiaoxing; }}//文件内容:
sadsadsadsa
123.00 1234.34 213
123.00 1267.34 12
4545.00 6767.34 76
//测试结果:
123.0 1234.34 213.0
123.0 1267.34 12.0
4545.0 6767.34 76.0

请采纳。