英文:
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<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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论