【发布时间】:2013-02-25 16:41:26
【问题描述】:
我编写了用于从文本文件传递整数输入的 Java 代码,例如1 10 39 59 20 60 38 当有空格时我必须拆分字符串。
input.txt 的输入在一行中给出
我的代码是:
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
String calc;
calc = key.toString();
ArrayList<Integer> keys = new ArrayList<Integer>();
String[] data = calc.split(" ");
for (String s : data) {
int intData = Integer.parseInt(s);
keys.add(intData);
}
int val = 0;
for (int a : keys) {
// some tasks
}
}
分割线后,我将分隔值用于不同的任务。我的问题是如何拆分位于同一文件中的所有值(值也在不同的行中)并将它们存储在一个数组中?
假设如果下面是input.txt中给定的输入,如何将所有的值拆分并存储到一个数组中?
输入示例:
1 4 92 58 30 82
49 50 38 30 29 20
...
预期输出:
array1="1,4,92,58,30,82,49,50,38,30,29,20, .."
当我将我的代码用于上述输入时,只考虑输入文件的最后一行 - 所有前面的行都被忽略。
【问题讨论】:
-
这个函数是如何被调用的?