加载二维棋盘

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

Loading two-dimensional board

问题

我有一个问题涉及这个数组:

    1.823803 1.000000 
    2.214117 1.000000 
    4.356716 1.000000 
    4.455463 1.000000 
    3.467892 1.000000 
    2.480369 1.000000 
    3.273540 1.000000 
    3.281274 1.000000 
    0.205179 1.000000 
    2.103515 1.000000 
    -0.057308 1.000000 
    1.794524 1.000000 
    3.160924 2.000000 
    2.856910 1.000000 
    2.247974 2.000000 
    1.953566 1.000000 
    4.241937 1.000000 
    1.782172 1.000000 
    4.869065 1.000000 
    2.090794 1.000000 
    1.663878 1.000000 
    3.157155 1.000000 
    3.501306 1.000000 
    2.066036 1.000000 
    4.793069 1.000000 
    2.484362 1.000000 
    2.201043 2.000000 
    4.189059 1.000000 

我需要从文件中加载它到两个单独的板块,或者一个二维板块。我不知道怎么做。它们必须被分成 1 和 2。有人愿意帮我吗?
英文:

i have a problem with this array:

1.823803 1.000000 
2.214117 1.000000 
4.356716 1.000000 
4.455463 1.000000 
3.467892 1.000000 
2.480369 1.000000 
3.273540 1.000000 
3.281274 1.000000 
0.205179 1.000000 
2.103515 1.000000 
-0.057308 1.000000 
1.794524 1.000000 
3.160924 2.000000 
2.856910 1.000000 
2.247974 2.000000 
1.953566 1.000000 
4.241937 1.000000 
1.782172 1.000000 
4.869065 1.000000 
2.090794 1.000000 
1.663878 1.000000 
3.157155 1.000000 
3.501306 1.000000 
2.066036 1.000000 
4.793069 1.000000 
2.484362 1.000000 
2.201043 2.000000 
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

public static double[][] readTableFromFile(File file) throws FileNotFoundException {
    try (Scanner scan = new Scanner(file)) {
        scan.useLocale(Locale.US);

        List<Double> data = new ArrayList<>();

        while (scan.hasNextDouble())
            data.add(scan.nextDouble());

        double[][] table = new double[data.size() / 2][2];
        Iterator<Double> it = data.iterator();
        int row = 0;

        while (it.hasNext()) {
            table[row][0] = it.next();
            table[row++][1] = it.next();
        }

        return table;
    }
}
英文:

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

public static double[][] readTableFromFile(File file) throws FileNotFoundException {
    try (Scanner scan = new Scanner(file)) {
        scan.useLocale(Locale.US);

        List&lt;Double&gt; data = new ArrayList&lt;&gt;();

        while (scan.hasNextDouble())
            data.add(scan.nextDouble());

        double[][] table = new double[data.size() / 2][2];
        Iterator&lt;Double&gt; it = data.iterator();
        int row = 0;

        while (it.hasNext()) {
            table[row][0] = it.next();
            table[row++][1] = it.next();
        }

        return table;
    }
}

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

发表评论

匿名网友

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

确定