问题描述
这是我的jsoup代码:
Here is my jsoup code:
for (i = 0; i < j; i++) {
if (!imageElement.get(i).attr("abs:src").isEmpty()) {
imagesSource[i] = imageElement.get(i).attr("abs:src");
titlesSource[i] = titleElement.get(i).attr("title");
System.out.println(imagesSource[i]);
System.out.println(titlesSource[i]);
} else {
imagesSource[i] = imageElement.get(i).attr("abs:image-src");
titlesSource[i] = titleElement.get(i).attr("title");
System.out.println(imagesSource[i]);
System.out.println(titlesSource[i]);
}
i++;
}
现在有48张图像和48个标题.打印后我应该得到96.但是,我只打印了48个.
Now there are 48 images and 48 titles. I should get 96 after printing. However, I got 48 only in printing.
当我只打印标题时,我得到了48行.但是,当我同时打印标题和图像时,我应该得到96行打印,但是我只有48行.
When I only print title, I got 48. However, when I print both titles and images I should get 96 lines printed, but I only get 48.
为什么?
推荐答案
这不是jsoup问题.
您的循环将i
递增2
:
It is not a jsoup issue.
Your loop increments i
by 2
:
for(i=0; i < j; i++){
...
i++;
}
因此,您可以在两个元素之间跳过一个元素.
删除i++;
.
So you skip one element on two.
Remove the i++;
.
通常来说,在循环增量表达式之外增加循环初始化器中使用的变量绝不是一个好主意.
Generally speaking, it is never a good idea to increment the variable used in the loop initializer outside of the loop increment expression.
这篇关于为什么要打印所有结果的一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!