gpt4 book ai didi

java - 如何使用 for 循环迭代数组列表并每 X 次执行一个操作? java

转载 作者:行者123 更新时间:2023-11-30 06:30:52 26 4
gpt4 key购买 nike

我正在尝试迭代由解析的 HTML 行组成的字符串 ArrayList。如何使用双 for 循环打印 0-353,同时每 4 打印一行?感谢大家的帮助

我必须做的是转到每一行,检查它是否是世界、国家、成员或 Activity 。然后,我会将所有 4 个数据添加到一个对象中,每次完成填充该对象时,我都必须启动一个新对象以添加到对象的另一个数组列表中。

<a id='slu-world-301' class='server-list__world-link'        href='http://oldschool.runescape.com/game?world=301'>Old School 1</a>
<td class='server-list__row-cell server-list__row-cell--country server-list__row-cell--US'>United States</td>
<td class='server-list__row-cell server-list__row-cell--type'>Free</td>
<td class='server-list__row-cell'>Trade - Free</td>
<a id='slu-world-302' class='server-list__world-link' href='http://oldschool.runescape.com/game?world=302'>Old School 2</a>
<td class='server-list__row-cell server-list__row-cell--country server-list__row-cell--GB'>United Kingdom</td>
<td class='server-list__row-cell server-list__row-cell--type'>Members</td>
<td class='server-list__row-cell'>Trade - Members</td>
<a id='slu-world-303' class='server-list__world-link' href='http://oldschool.runescape.com/game?world=303'>Old School 3</a>
<td class='server-list__row-cell server-list__row-cell--country server-list__row-cell--DE'>Germany</td>
<td class='server-list__row-cell server-list__row-cell--type'>Members</td>
<td class='server-list__row-cell'>-</td>

我当前的代码:

for(int i=1;i<(strs.size() / 4) + 1;i++){
for(int j=0;j<4;j++){
System.out.println(???? WHAT DO I PUT HERE ????)
}
System.out.println("-----------------");
}

最佳答案

我建议使用 % 这个余数运算符
引用https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

0 % 4 = 0   
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0



for(int i = 0; i < strs.size(); i++){
System.out.println(i);
if (i%4 == 3) {
System.out.println("---------");
}
}

输出为

0
1
2
3
---------
4
5
6
7
---------
8
9
10
11
---------
<小时/>

如果代码如下

for(int i=0;i<strs.size();i++) {
if(i%4 ==0) {
System.out.println("---------");
}
System.out.println(i);
}

输出将是

 ------------ //will more one ----- because 0 % 4 = 0
0
1
2
3
------------
4
5
6
7
------------
8
9
<小时/>

关于java - 如何使用 for 循环迭代数组列表并每 X 次执行一个操作? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46166532/

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