我在尝试用Java编写语法以执行我想要对此文件执行的操作时遇到了麻烦
我正在做的是读取文件并且我想将文件的特定行添加到2D数组的元素中,我在下面写了一些伪代码以帮助理解我想要做什么
try
{
FileInputStream fStream = new FileInputStream(fileName);
DataInputStream inStream = new DataInputStream(fStream);
BufferedReader brRead = new BufferedReader (new InputStreamReader(inStream));
String line;
while ((line = brRead.readLine()) !=null)
{
//Lines 1-3
//{
// Add line 1 into element 0,0 of my array
// Add line 2 into element 0,1 of my array
// Add line 3 into element 0,2 of my array
//}
//Lines 4-10
//{
// Add line 4 into element 1,0 of my array
// Add line 5 into element 1,1 of my array
// ect..
//}
}
}
catch (Exception e)
{
}
}
答案 0 :(得分:0)
假设2d阵列都是3 x 3(没有清楚解释)。 算法和代码可能类似于下面的内容,这非常简单。
try
{
FileInputStream fStream = new FileInputStream(fileName);
DataInputStream inStream = new DataInputStream(fStream);
BufferedReader brRead = new BufferedReader (new InputStreamReader(inStream));
String line;
int lineCount = 0;
int mod9 = -1;
int firstIndex = -1;
int secondIndex = -1;
double[][] array = new double[3][3];
while ((line = brRead.readLine()) !=null)
{
//Lines 1-3
//{
// Add line 1 into element 0,0 of my array
// Add line 2 into element 0,1 of my array
// Add line 3 into element 0,2 of my array
//}
mod9 = lineCount % 9 ;
firstIndex = mod9 / 3;
secondIndex = mod9 % 3;
array[firstIndex] += Double.parseDouble(line);
lineCount++;
//Lines 4-10
//{
// Add line 4 into element 1,0 of my array
// Add line 5 into element 1,1 of my array
// ect..
//}
}
}
catch (Exception e)
{
}