gpt4 book ai didi

java - 在 reducer 的 for 循环中获取编译错误 "Can only iterate over an array or an instance of java.lang.Iterable"

转载 作者:可可西里 更新时间:2023-11-01 15:02:13 25 4
gpt4 key购买 nike

在 reducer 的 for 循环中出现编译错误“Can only iterate over an array or an instance of java.lang.Iterable”。

public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> Output, Reporter arg3)
throws IOException {
// TODO Auto-generated method stub

int sum = 0;
for (IntWritable val : values) {
sum += val.get();

在上面的代码中,在 “for (IntWritable val : values)” 处出现编译错误“Can only iterate over an array or an instance of java.lang.Iterable”。如何使用循环?

最佳答案

或者传递一个 Iterable<IntWritable>而不是 Iterator<IntWritable>方法,或使用 hasNext()next()遍历 Iterator 的元素:

 while (values.hasNext()) {
IntWritable val = values.next();
...
}

关于java - 在 reducer 的 for 循环中获取编译错误 "Can only iterate over an array or an instance of java.lang.Iterable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36078389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com