Java问题 iterator
publicintsumArrayRecursive(int[]numbers){intsum=0;for(inti=0;i<numbers.length;i++){su...
public int sumArrayRecursive(int[] numbers)
{
int sum = 0;
for(int i = 0; i < numbers.length; i++)
{
sum = sum + numbers[i];
}
return sum;
}
public int sumArrayRecursive(List<Integer> numbers)
{
int sum = 0;
for(int i : numbers)
{
sum = sum + i;
}
return sum;
}
需要改写成使用iterator的方法 展开
{
int sum = 0;
for(int i = 0; i < numbers.length; i++)
{
sum = sum + numbers[i];
}
return sum;
}
public int sumArrayRecursive(List<Integer> numbers)
{
int sum = 0;
for(int i : numbers)
{
sum = sum + i;
}
return sum;
}
需要改写成使用iterator的方法 展开
1个回答
展开全部
public int sumArrayRecursive(List<Integer> numbers) {
int sum = 0;
Iterator<Integer> it = numbers.iterator();
while (it.hasNext()) {
sum = sum + it.next();
}
return sum;
}
更多追问追答
追问
上面一个怎么做呢。
追答
public int sumArrayRecursive(int[] numbers) {
int sum = 0;
List list = Arrays.asList(numbers);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
sum = sum + it.next();
}
return sum;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询