问题描述
解决方案
简单的写了一下,希望对你有帮助:
public class Test {
// 数组列数
private static final int COLUMN_COUNT = 3;
public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
"d://test.txt")));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line).append("n");
}
System.out.println("=============转一维数组================");
String[] singleArray = sb.toString().split("\s+");
// 遍历一维数组
for (String str1 : singleArray) {
System.out.println(str1);
}
System.out.println("n=============转二维数组================");
int rows = singleArray.length / COLUMN_COUNT;// 数组行数
int num = -1;
String[][] doubleArray = new String[rows][COLUMN_COUNT];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < COLUMN_COUNT; j++) {
doubleArray[i][j] = singleArray[++num];
}
}
// 遍历二维数组
for (int i = 0; i < rows; i++) {
for (int j = 0; j < COLUMN_COUNT; j++) {
System.out.print(doubleArray[i][j] + "t");
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
解决方案二:
String[] r=str.split("s+");
解决方案三:
在循环里定义一个长度为3的数组接一行的值,再把数组放到list里 循环结束赋值结束
时间: 2024-10-03 18:52:23