加载二维棋盘

huangapple 未分类评论51阅读模式
英文:

Loading two-dimensional board

问题

  1. 我有一个问题涉及这个数组:
  2. 1.823803 1.000000
  3. 2.214117 1.000000
  4. 4.356716 1.000000
  5. 4.455463 1.000000
  6. 3.467892 1.000000
  7. 2.480369 1.000000
  8. 3.273540 1.000000
  9. 3.281274 1.000000
  10. 0.205179 1.000000
  11. 2.103515 1.000000
  12. -0.057308 1.000000
  13. 1.794524 1.000000
  14. 3.160924 2.000000
  15. 2.856910 1.000000
  16. 2.247974 2.000000
  17. 1.953566 1.000000
  18. 4.241937 1.000000
  19. 1.782172 1.000000
  20. 4.869065 1.000000
  21. 2.090794 1.000000
  22. 1.663878 1.000000
  23. 3.157155 1.000000
  24. 3.501306 1.000000
  25. 2.066036 1.000000
  26. 4.793069 1.000000
  27. 2.484362 1.000000
  28. 2.201043 2.000000
  29. 4.189059 1.000000
  30. 我需要从文件中加载它到两个单独的板块,或者一个二维板块。我不知道怎么做。它们必须被分成 1 2。有人愿意帮我吗?
英文:

i have a problem with this array:

  1. 1.823803 1.000000
  2. 2.214117 1.000000
  3. 4.356716 1.000000
  4. 4.455463 1.000000
  5. 3.467892 1.000000
  6. 2.480369 1.000000
  7. 3.273540 1.000000
  8. 3.281274 1.000000
  9. 0.205179 1.000000
  10. 2.103515 1.000000
  11. -0.057308 1.000000
  12. 1.794524 1.000000
  13. 3.160924 2.000000
  14. 2.856910 1.000000
  15. 2.247974 2.000000
  16. 1.953566 1.000000
  17. 4.241937 1.000000
  18. 1.782172 1.000000
  19. 4.869065 1.000000
  20. 2.090794 1.000000
  21. 1.663878 1.000000
  22. 3.157155 1.000000
  23. 3.501306 1.000000
  24. 2.066036 1.000000
  25. 4.793069 1.000000
  26. 2.484362 1.000000
  27. 2.201043 2.000000
  28. 4.189059 1.000000

I have to load it from a file to two single boards or one two dimensional board. I don't know how to do it. They have to divide it into 1 and 2. Anybody want to help me?

答案1

得分: 0

有很多方法可以在Java中读取文本文件。其中之一是使用Scanner

  1. public static double[][] readTableFromFile(File file) throws FileNotFoundException {
  2. try (Scanner scan = new Scanner(file)) {
  3. scan.useLocale(Locale.US);
  4. List<Double> data = new ArrayList<>();
  5. while (scan.hasNextDouble())
  6. data.add(scan.nextDouble());
  7. double[][] table = new double[data.size() / 2][2];
  8. Iterator<Double> it = data.iterator();
  9. int row = 0;
  10. while (it.hasNext()) {
  11. table[row][0] = it.next();
  12. table[row++][1] = it.next();
  13. }
  14. return table;
  15. }
  16. }
英文:

There're many ways to read text file in Java. One of them is to use Scanner:

  1. public static double[][] readTableFromFile(File file) throws FileNotFoundException {
  2. try (Scanner scan = new Scanner(file)) {
  3. scan.useLocale(Locale.US);
  4. List&lt;Double&gt; data = new ArrayList&lt;&gt;();
  5. while (scan.hasNextDouble())
  6. data.add(scan.nextDouble());
  7. double[][] table = new double[data.size() / 2][2];
  8. Iterator&lt;Double&gt; it = data.iterator();
  9. int row = 0;
  10. while (it.hasNext()) {
  11. table[row][0] = it.next();
  12. table[row++][1] = it.next();
  13. }
  14. return table;
  15. }
  16. }

huangapple
  • 本文由 发表于 2020年4月4日 02:25:56
  • 转载请务必保留本文链接:https://java.coder-hub.com/61018202.html
匿名

发表评论

匿名网友

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

确定