如何在每行的集合 #(10) 中输出一维数组



我知道我可以一起使用许多 if 语句,但我认为这很烦人,有没有更好的方法?

    for(index=0; index<alpha.length; index++)
    {
        System.out.print(alpha[index]+ "");
        if (index == 9)
            System.out.println();
        if (index == 19)
            System.out.println();
        if (index == 29)
            System.out.println();
        if (index == 39)
            System.out.println();
    }
if ((index + 1) % 10 == 0) 

这(%)是分裂的其余部分。

使用内部循环(从 0 到 9(包括 0 和 9)迭代)。

像这样使用 % 运算符:

for(index=0; index<alpha.length; index++)
{
    System.out.print(alpha[index]+ "");
    if ( ( ( index + 1 ) % 10 ) == 0 ) {
        System.out.println();
    }
}
for(index=0; index<alpha.length; index++)
{
    System.out.print(alpha[index]+ "");
    if (index % 10 == 9)
        System.out.println();
}

最新更新