好吧,我已经难倒了最好的方法来做到这一点,我写了代码从txt文件中读取代码行为List。然后,我可以打印特定部分或将其转换为对象数组。但是,最终我想只有一个可以在C/C++中经常看到的2d int数组。谈到java时,我非常青睐,只是本周早些时候才开始。我一直喜欢它,直到在运行时创建动态二维数组为止。你们中的任何一个人都可以提出一个从我当前卡住的地方进入2d int数组的好方法。我正要使用'toChar'将它转换为char数组,然后将(value @ index-48)存储到它的相应位置,但对我来说这似乎很不起眼。列表<String> ----> int [] [] arr
====更新==========================
嗯,感谢所有的答复,但我只是想出了如何使用双打来做到这一点,所以对于其他人来说,在这里你走了。我仍然宁愿int,因为我已经使用这种类型构建了我的其他matrixops类,但Double不应该是我猜测的问题。
package uaa.cse215;
import java.io.*;
import java.util.*;
public class ReadMatrix {
private Double[][] A;
private Double[][] B;
private int count;
public int filedir(String matrix) throws Exception{
Double[][] Temp;
String[] arr;
BufferedReader rd = new BufferedReader(new FileReader(matrix));
String s;
List<String> textFile = new ArrayList<String>();
while ((s=rd.readLine())!=null) {
textFile.add(s);
}
String splitarray[] = textFile.get(0).split(" ");//run once to grab # cols
int rows = textFile.size();//number of rows
int cols = splitarray.length;//number of cols
Temp = new Double[rows][cols]; // now can initiate array
for (int i=0; i<rows; i++) {
s = textFile.get(i);
arr = s.split(" ");
for (int j=0; j<cols; j++) {
Temp[i][j] = Double.parseDouble(arr[j]);
}
}
count++;
if (count == 1){
A = Temp;
}
else
B = Temp;
rd.close();
return(1);
}
}
你在这2个小时前在哪里哈哈。我一直在寻找这样的东西,并与评论!非常感谢 –