[英]Print a list in lines of 8 (java)
I need to print a list in lines of 8. Say if the list was integers long it would print 8 on the first row and 2 on the next. 我需要在8行中打印一个列表。假设该列表的长度是整数,它将在第一行打印8,在第二行打印2。 Here is my code: 这是我的代码:
for(int i = 0; i < size; i++){
System.out.println(list[i] + " ");
}
How could I print it in lines of 8 instead of printing one per line? 如何以8行打印而不是每行打印一次?
Use StringBuilder
: 使用StringBuilder
:
StringBuilder sb = new StringBuilder();
sb.append(list[0]).append(" ");
for (int i = 1 ; i < size; i++) {
sb.append(list[i]).append(" ");
if ( (i + 1) % 8 == 0 ) { sb.append("\n"); }
}
System.out.print(sb.toString());
Every answer I've seen until now has a bug in where it breaks. 到目前为止,我所看到的每个答案都存在错误。 Use the following: 使用以下内容:
int[] list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
int br = 8;
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
if (i % br == (br - 1))
System.out.println();
}
Or: 要么:
int br = 8;
for (int i = 0; i < list.length; i++) {
if (i > 0 && i % br == 0)
System.out.println();
System.out.print(list[i] + " ");
}
Output: 输出:
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18
Here are some ways (taken from other answers to this question) that this cannot be done. 这是一些无法完成的方法(从该问题的其他答案中获取)。
i % 8 == 0
. 当i % 8 == 0
时中断。 This doesn't work because 0 % 8 == 0
is true, which means this will cause a break after the first element. 这是行不通的,因为0 % 8 == 0
为true,这意味着在第一个元素之后将导致中断。 i > 0 && (i % 8 == 0)
. 首先打印项目,然后在i > 0 && (i % 8 == 0)
时中断。 This does not work because arrays are zero-indexed (eg i
does not equal 8
until the 9th element. In other words, you'll have 9 items on the first line. However, this works if you check for the break before printing the element. 这不起作用,因为数组的索引为零(例如,直到第9个元素, i
不等于8
换句话说,第一行中将有9个项目。但是,如果在打印元件。 1
. 打印出第一项,然后从1
开始循环索引。 This suffers from the same problem as the previous answer; 这遭受了与先前答案相同的问题。 you'll still always be one index behind. 您仍然总是落后于一个索引。 You can use a StringBuilder
to make this marginally faster, but it's more important to get a correct answer than a fast one. 您可以使用StringBuilder
稍微加快速度,但获得正确答案比快速答案更为重要。
for (int i = 0; i < size; i++) {
System.out.print(list[i]);
if (i % 8 == 7) {
System.out.println();
} else {
System.out.print(" ");
}
}
if (size % 8 != 0) {
System.out.println();
}
Technically, print("\\n")
and println()
do not necessarily have the same effect, which is why the above doesn't just do System.out.print((i % 8 ==7) ? "\\n" : " ")
. 从技术上讲, print("\\n")
和println()
不一定具有相同的效果,这就是为什么上面的方法不只是System.out.print((i % 8 ==7) ? "\\n" : " ")
。
The extra if is to ensure that the last line is terminated even if it has fewer than 8 elements. 额外的if是即使最后一行少于8个元素,也要确保将其终止。
To use Guava... Lists.partition breaks a list into a list of lists with a max length. 要使用Guava ... Lists.partition可以将列表分成最大长度的列表。
List<String> myList;
for (List<String> subs : Lists.partition(myList, 8)){
System.out.println(Joiner.on(" ").join(subs));
}
for (int i = 0; i < size; i++)
{
if ((i % 8 == 0) && (i > 0))
{
System.out.println();
}
System.out.print(list[i] + " ");
}
for(int i = 0; i < size; i++){
System.out.println(list[i] + "\n");
}
\\n is for new line. \\ n用于换行。
for (int nI = 0; nI < list.size(); nI++)
{
System.out.print(list[nI] + " ");
// new line if you hit 8th, 16th, etc. item in the list
if (nI > 1 && ((nI+1) % 8 == 0))
System.out.print("\n");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.