Java 打印每行 10 个元素的数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2661489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print an array elements with 10 elements on each line
提问by user319570
I just created an array with 100 initialized values and I want to print out 10 elements on each line so it would be somthing like this
我刚刚创建了一个包含 100 个初始化值的数组,我想在每一行打印出 10 个元素,所以它会是这样的
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16
...26
this is the code I used and I managed to do it for the first 10 elements but I couldn't figure out how to do it for the rest
这是我使用的代码,我设法为前 10 个元素执行此操作,但我不知道如何为其余元素执行此操作
public static void main(String[] args) {
int[] numbers = { 0,1,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
int i, count = 0;
for (i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
count++;
if (count == 9)
for (i = 9; i < numbers.length; i++)
System.out.println(numbers[i] + " ");
}
}
回答by polygenelubricants
int[] numbers = new int[100];
for (int i = 0; i < numbers.length; i++) {
if (i % 10 == 0 && i > 0) {
System.out.println();
}
System.out.print(numbers[i] + " ");
}
This prints a newline before printing numbers[i]
where i % 10 == 0
and i > 0
. %
is the mod operator; it returns the remainder if i / 10
. So i % 10 == 0
when i = 0, 10, 20, ...
.
这会在打印numbers[i]
wherei % 10 == 0
和之前打印换行符i > 0
。%
是 mod 运算符;如果 ,则返回余数i / 10
。所以i % 10 == 0
当i = 0, 10, 20, ...
.
As for your original code, you can make it work with a little modification as follows:
至于你的原始代码,你可以做一些修改,如下所示:
int count = 0;
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
count++;
if (count == 10) {
System.out.println();
count = 0;
}
}
Basically, count
is how many numbers you've printed in this line. Once it reaches 10, you print the newline, and then reset it back to 0, because you're starting a new line, and for that line, you haven't printed any numbers (yet).
基本上,count
是您在此行中打印了多少个数字。一旦达到 10,您就打印换行符,然后将其重置回 0,因为您要开始一个新行,而对于该行,您还没有打印任何数字(还)。
Note that in above two solutions, an extra space is printed at the end of each line. Here's a more flexible implementation that only uses separators (horizontal and vertical) when necessary. It's only slightlymore complicated.
请注意,在上述两种解决方案中,每行末尾都会打印一个额外的空格。这是一个更灵活的实现,仅在必要时使用分隔符(水平和垂直)。它只是稍微复杂一些。
static void print(int[] arr, int W, String hSep, String vSep) {
for (int i = 0; i < arr.length; i++) {
String sep =
(i % W != 0) ? hSep :
(i > 0) ? vSep :
"";
System.out.print(sep + arr[i]);
}
System.out.println(vSep);
}
If you call this say, as print(new int[25], 5, ",", ".\n");
, then it will print 25 zeroes, 5 on each line. There's a period (.
) at the end of each line, and a comma (,
) between zeroes on a line.
如果您将此称为 as print(new int[25], 5, ",", ".\n");
,那么它将打印 25 个零,每行 5 个。.
每行末尾有一个句点 ( ),一行中的,
零之间有一个逗号 ( )。
回答by Matthew T. Staebler
All you are going to have to do is to print out a newline after every ten numbers.
您所要做的就是在每十个数字后打印出一个换行符。
for (i = 0; i < numbers.length; ++i)
{
System.out.print(number[i]);
if (i % 10 == 9)
{
System.out.println();
}
else
{
System.out.print(" ");
}
}
回答by rsp
Why do you use 2 nested loops where you only need to remember at which places you need to output a linebreak? Also using the same variable i
for both loops will not do what you expect.
为什么要使用 2 个嵌套循环,只需记住在哪些位置需要输出换行符?同样,i
对两个循环使用相同的变量也不会达到您的预期。
How about:
怎么样:
for (i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
count++;
if (count == 10)
System.out.print("\n");
count = 0;
}
}
回答by ilkin
I know this is an old question, but I just wanted to answer. In java 8 you can do this in one line. Arrays.stream(arr).forEach(s->System.out.print(s%10 > 0 ? s+" ":"\n"));
我知道这是一个老问题,但我只是想回答。在 java 8 中,您可以在一行中完成此操作。Arrays.stream(arr).forEach(s->System.out.print(s%10 > 0 ? s+" ":"\n"));