如何从此(在给定链接下方)文件(.txt)中读取输入并将其存储在2-d数组或2d向量中?
输入文件链接:
http://spark-public.s3.amazonaws.com/algo1/programming_prob/kargerMinCut.txt
P.S。 - 我不想将它们存储为字符串。
答案 0 :(得分:0)
public int[][] readFileToArray(String nameoffile){
int[][] outArray = null;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("C:\\Users\\U383250\\Desktop\\test.txt"));
String line = null;
String[] lineVariables = null;
while ((line = reader.readLine()) != null ) {
lineVariables = line.split("\n");
if(lineVariables!=null && lineVariables.length>0){
outArray = new int[lineVariables.length][100]; // put you line length here instead of 100
int i = 0;
for(String string : lineVariables){
String[] eachLine = string.split("\\s+");
int j = 0;
for(String inLine : eachLine){
outArray[i][j] = Integer.parseInt(inLine);
j++;
}
i++;
}
}
}
} catch (IOException e) {
System.err.println(e);
}
finally{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return outArray;
}
您应该根据自己的逻辑/要求指定二维数组的大小
答案 1 :(得分:-3)
定义我们编写的2D数组
int x[5]//(rows)[5]//(coloums)