for(int j = 0; j < arrayList.size(); j++) {
System.out.print(arrayList.get(j));
}
我想打印 5 个一组,但代码看起来如何?我是否必须将元素存储到变量中并以这种方式打印它们?
在每 5 个元素之后添加一个简单的换行符怎么样?
for (int j = 0; j < arrayList.size(); j++){
System.out.print(arrayList.get(j) + " ");
if (j % 5 == 4) {
System.out.print("\n");
}
}
您还可以使用subList(int fromIndex, int toIndex)来打破它们。
for(int j = 0; j < arrayList.size(); j+=5){
for (int i = 0; i < 5; i++){
//Inside this loop you can do whatever you want, concatenate...
System.out.print(arrayList.get(i+j));
}
System.out.println("");
}
for(int j = 0; j < arrayList.size(); j++){
if ((j != 0) && (j % 4 == 0) { System.out.println(""); }
System.out.print(arrayList.get(j));
}
Oops... Just realized lots of people answered this already...