将两行整数文本转换为二维数组。

huangapple go评论168阅读模式
英文:

Convert 2 text line of integers into 2D array

问题

我想将这些文本行转换为2D数组,使用BufferReader。有什么帮助吗?

英文:

I have some text lines like this

  1. 14 14 14 14
  2. 32 32 32 32

I want to convert into 2D array with BufferReader. Any help?

答案1

得分: 0

  1. 我希望你正在寻找将字符串数组转换为缓冲区的方法
  2. public int[][] convertTo2DArray(String filename) {
  3. int[][] array = null;
  4. try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
  5. String line;
  6. int row = 0;
  7. while ((line = reader.readLine()) != null) {
  8. String[] elements = line.split(" ");
  9. if (array == null) {
  10. array = new int[elements.length][];
  11. }
  12. array[row] = new int[elements.length];
  13. for (int i = 0; i < elements.length; i++) {
  14. array[row][i] = Integer.parseInt(elements[i]);
  15. }
  16. row++;
  17. }
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. return array;
  22. }
  23. String filename = "path/to/your/file.txt";
  24. int[][] result = convertTo2DArray(filename);
英文:

I am hoping you are looking for convert string array in buffer

  1. public int[][] convertTo2DArray(String filename) {
  2. int[][] array = null;
  3. try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
  4. String line;
  5. int row = 0;
  6. while ((line = reader.readLine()) != null) {
  7. String[] elements = line.split(&quot; &quot;);
  8. if (array == null) {
  9. array = new int[elements.length][];
  10. }
  11. array[row] = new int[elements.length];
  12. for (int i = 0; i &lt; elements.length; i++) {
  13. array[row][i] = Integer.parseInt(elements[i]);
  14. }
  15. row++;
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. return array;
  21. }
  22. String filename = &quot;path/to/your/file.txt&quot;;
  23. int[][] result = convertTo2DArray(filename);

答案2

得分: 0

通过使用BufferedReader的lines()方法拆分您的行,然后可以使用映射将数字按空格拆分。(使用这个正则表达式可以在有多个空格字符的情况下拆分)

  1. String input = "14 14 14 14\n32 32 32 32\n";
  2. BufferedReader reader = new BufferedReader(new StringReader(input));
  3. int[][] array = reader.lines()
  4. .map(line -> Arrays.stream(line.split("\\s+"))
  5. .mapToInt(Integer::parseInt)
  6. .toArray())
  7. .toArray(int[][]::new);
英文:

By splitting your lines with the lines() method of BufferedReader, you can then use a map to split numbers with spaces. (using this regular expression allows to split even if there is more than one space character)

  1. String input = &quot;14 14 14 14\n32 32 32 32\n&quot;;
  2. BufferedReader reader = new BufferedReader(new StringReader(input));
  3. int[][] array = reader.lines()
  4. .map(line -&gt; Arrays.stream(line.split(&quot;\\s+&quot;))
  5. .mapToInt(Integer::parseInt)
  6. .toArray())
  7. .toArray(int[][]::new);

huangapple
  • 本文由 发表于 2023年6月6日 14:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定