我是新来的火花,我正在尝试将字数映射到计数和错误,以实现节省空间算法。对于我需要保留其计数和错误的每个单词。我的代码如下。问题是我无法访问tuple2中的整数数组中的元素。
JavaPairRDD<String, Integer[]> ones = words.mapToPair(new PairFunction<String, String, Integer[]>() {
@Override
public Tuple2<String, Integer[]> call(String s) {
Integer[] defaults = {1,0};
return new Tuple2<String, Integer[]>(s, defaults);
}
});
List<Tuple2<String,Integer[]>> output = ones.collect();
for (Tuple2<?, ?> tuple : output) {
Integer temp = (Integer) tuple._2()[0]; ///ERROR, HOW CAN I ACCESS ARRAY ELEMENT???
System.out.println(tuple._1() + ": " + tuple._2()+" "+temp[0]);
}
答案 0 :(得分:1)
改变这个:
for (Tuple2<?, ?> tuple : output) {
Integer temp = (Integer) tuple._2()[0]; ///ERROR, HOW CAN I ACCESS ARRAY ELEMENT???
System.out.println(tuple._1() + ": " + tuple._2()+" "+temp[0]);
}
到
for (Tuple2<String, Integer[]> tuple : output) {
Integer temp = tuple._2()[0];
System.out.println(tuple._1() + ": " + tuple._2() + " " + temp[0]);
}