3条回答 默认 最新
关注 简单的写了一下,希望对你有帮助:
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(); } } } }
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记
- ¥15 保护模式-系统加载-段寄存器