0
for(int j = 0; j < arrayList.size(); j++) {
    System.out.print(arrayList.get(j));
}

我想打印 5 个一组,但代码看起来如何?我是否必须将元素存储到变量中并以这种方式打印它们?

4

4 回答 4

7

在每 5 个元素之后添加一个简单的换行符怎么样?

for (int j = 0; j < arrayList.size(); j++){
   System.out.print(arrayList.get(j) + " ");
   if (j % 5 == 4) {
       System.out.print("\n");
   }
}
于 2013-09-03T16:47:33.907 回答
0

您还可以使用subList(int fromIndex, int toIndex)来打破它们。

于 2013-09-03T16:48:46.180 回答
0
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("");
}
于 2013-09-03T16:48:06.957 回答
0
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...

于 2013-09-03T16:53:19.503 回答