我在特定文件中有n列和m行我怎么能但是每个col在不同的数组中? *行数和列数可以更改
String readline = null;
readline = read.readLine();
String[] getlength = readline.split(" ");
cols = getlength.length;
while (readline != null) {
readline = read.readLine();
rows++;
}
// Array of data
int[][] data = new int[rows][cols];
// New bufferedReader to put the cursor on the top of the file
read = new BufferedReader(new FileReader("1.txt"));
// for loop to skip first three rows
for (int i = 1; i <= 3; i++) {
read.readLine();
}
// to set the file Data into the array
String line = null;
line = read.readLine();
do {
readData = line.split(" ");
for (int j = 0; j < readData.length; j++) {
data[indexx][j] = Integer.parseInt(readData[j]);
}
indexx++;
line = read.readLine();
} while (line != null);
答案 0 :(得分:0)
如果您不知道正在阅读的表格中的列数,则应使用ArrayList
。一种可能的方式是:
[...]
ArrayList result = new ArrayList<int[]>();
line = read.readLine();
do {
readData = line.split(" ");
int[] row = new int[readData.length];
for(int i = 0; i < readData.length; i++) {
row[i] = Integer.parseInt(readData[i]);
}
result.add(row);
} while (line != null);
现在,如果您需要将结果作为数组,则可以执行以下转换:
int[] data = result.toArray(new int[result.size()]);