Java 从文本文件生成二维双数组
我有一个文本文件Java 从文本文件生成二维双数组,java,arrays,text,double,Java,Arrays,Text,Double,我有一个文本文件 0.4658 0 3 0.4095 0 3 0.4594 0 3 0.4297 0 3 0.3963 0 3 0.4232 0 3 0.4633 0 3 0.5384 0 3 0.5042 0 3 0.4328 0 3 我想读入一个二维双数组,如下所示 {{0.4658, 0, 3}, {0.4095, 0, 3}, ... (and so on) {0.4328, 0, 3}} 我有以下代码: public static void main(String[]
0.4658 0 3
0.4095 0 3
0.4594 0 3
0.4297 0 3
0.3963 0 3
0.4232 0 3
0.4633 0 3
0.5384 0 3
0.5042 0 3
0.4328 0 3
我想读入一个二维双数组,如下所示
{{0.4658, 0, 3},
{0.4095, 0, 3},
... (and so on)
{0.4328, 0, 3}}
我有以下代码:
public static void main(String[] args) throws Exception{
double[][] ref = null;
ref = matrix("data80.in",10,3);
}
public static double[][] matrix(String filename, int size1, int size2) throws Exception {
double[][] matrix = null;
BufferedReader buffer = new BufferedReader(new FileReader(filename));
String line;
int row = 0;
while ((line = buffer.readLine()) != null) {
String[] vals = line.trim().split("\\s+");
if (matrix == null) {
matrix = new double[size1][size2];
}
for (int col = 0; col < size1; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
row++;
}
buffer.close();
return matrix;
}
publicstaticvoidmain(字符串[]args)引发异常{
double[]ref=null;
ref=矩阵(“数据80.in”,10,3);
}
公共静态双[]矩阵(字符串文件名,int-size1,int-size2)引发异常{
双[]矩阵=空;
BufferedReader buffer=新的BufferedReader(新文件读取器(文件名));
弦线;
int行=0;
而((line=buffer.readLine())!=null){
字符串[]vals=line.trim().split(\\s+);
if(矩阵==null){
矩阵=新的双[size1][size2];
}
对于(int col=0;col
但它一直给我一个边界外的例外,我不知道我会错在哪里。请帮忙。如果有人对我的问题也有更有效的解决方案,那将很有帮助您将2d矩阵定义为
matrix = new double[size1][size2];
这意味着有size1
行和size2
列,但在以下行中:
for (int col = 0; col < size1; col++) {
这是因为以下
for
循环:
for (int col = 0; col < size1; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
此外,无需在for
循环中进行null
检查,您可以删除它并在开始时初始化数组,例如:
public static double[][] matrix(String filename, int size1, int size2) throws Exception {
double[][] matrix = new double[size1][size2];;
BufferedReader buffer = new BufferedReader(new FileReader(filename));
String line;
int row = 0;
while ((line = buffer.readLine()) != null) {
String[] vals = line.trim().split("\\s+");
for (int col = 0; col < size2; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
row++;
}
buffer.close();
return matrix;
}
public static double[][]矩阵(字符串文件名,int-size1,int-size2)引发异常{
双精度[]矩阵=新的双精度[size1][size2];;
BufferedReader buffer=新的BufferedReader(新文件读取器(文件名));
弦线;
int行=0;
而((line=buffer.readLine())!=null){
字符串[]vals=line.trim().split(\\s+);
for(int col=0;col
关于如何将任意行数和任意列数的文本文件中包含的分隔数字数据放入双数据类型二维数组,这里还有另一个概念。您只需将路径和文件名传递给该方法。您还可以选择提供文件中使用的分隔符,默认方法是逗号(,)分隔符,因为它是最常用的分隔符之一。方法如下:
public static double[][] matrix(String filename, String... delimiterInFile) {
String delimiter = ","; // Default data delimiter in file.
// See if a optional data delimiter is supplied...
if (delimiterInFile.length > 0) { delimiter = delimiterInFile[0]; }
// Catch IO Exceptions
try {
//Place the contents of file into a ArrayList
List<String> list = Files.lines(Paths.get(filename)).collect(Collectors.toList());
// Get the greatest number of delimited columns contiained
// within the ArrayList the whole while ignoring blank lines
// (there could be blank lines in file). Our columns for our
// double 2D Array will be determined from this value. We also
// determine the true number of rows required (remember, no
// blank elements allowed so ArrayList.size() wont be good enough).
int r = 0, c = 0;
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals("")) {
int l = list.get(i).split(delimiter).length;
if (l > c) { c = l; }
r++;
}
}
// If we have nothing then the get outta here
// by returning null.
if (r == 0 || c == 0) { return null; }
// Declare and initialize a double data type 2D Array
double[][] array = new double[r][c];
// Fill the double type array...
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals("")) {
String[] data = list.get(i).split(delimiter);
for (int j = 0; j < data.length; j++) {
array[i][j] = Double.parseDouble(data[j]);
}
}
}
return array;
} catch (IOException ex) {
// Do what you want with the Exception...
ex.printStackTrace();
return null;
}
}
假设这个文件名为data.txt
,在您的
double[][] myData = matrix("data.txt", "\\s+");
for (int i = 0; i < myData.length; i++) {
String strg = "";
for (int j = 0; j < myData[0].length; j++) {
strg+= myData[i][j] + " ";
}
System.out.println(strg);
}
double[]myData=matrix(“data.txt”,“\\s+”);
对于(int i=0;i
请记住,对于非常大的数据文件(数十万行),我不建议使用这种方法。同时检查java.io.FileNotFoundException。
public static double[][] matrix(String filename, String... delimiterInFile) {
String delimiter = ","; // Default data delimiter in file.
// See if a optional data delimiter is supplied...
if (delimiterInFile.length > 0) { delimiter = delimiterInFile[0]; }
// Catch IO Exceptions
try {
//Place the contents of file into a ArrayList
List<String> list = Files.lines(Paths.get(filename)).collect(Collectors.toList());
// Get the greatest number of delimited columns contiained
// within the ArrayList the whole while ignoring blank lines
// (there could be blank lines in file). Our columns for our
// double 2D Array will be determined from this value. We also
// determine the true number of rows required (remember, no
// blank elements allowed so ArrayList.size() wont be good enough).
int r = 0, c = 0;
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals("")) {
int l = list.get(i).split(delimiter).length;
if (l > c) { c = l; }
r++;
}
}
// If we have nothing then the get outta here
// by returning null.
if (r == 0 || c == 0) { return null; }
// Declare and initialize a double data type 2D Array
double[][] array = new double[r][c];
// Fill the double type array...
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals("")) {
String[] data = list.get(i).split(delimiter);
for (int j = 0; j < data.length; j++) {
array[i][j] = Double.parseDouble(data[j]);
}
}
}
return array;
} catch (IOException ex) {
// Do what you want with the Exception...
ex.printStackTrace();
return null;
}
}
0.4658 0 3
0.4095 0 3
0.4594 0 3
0.4297 0 3
0.3963 0 3
0.4232 0 3
0.4633 0 3
0.5384 0 3
0.5042 0 3
0.4328 0 3
double[][] myData = matrix("data.txt", "\\s+");
for (int i = 0; i < myData.length; i++) {
String strg = "";
for (int j = 0; j < myData[0].length; j++) {
strg+= myData[i][j] + " ";
}
System.out.println(strg);
}