gpt4 book ai didi

java - 打印 10x10 表中列表的元素

转载 作者:行者123 更新时间:2023-12-01 18:34:14 25 4
gpt4 key购买 nike

我正在尝试在 10 x 10 表中打印 LinkedList 的 100 个元素。我有以下代码:

for (int a = 0; a < 10; a++) {
System.out.print(lList.get(a) + " ");
}//end for
System.out.println("");

for (int a = 10; a < 20; a++) {
System.out.print(lList.get(a) + " ");
}//end for
System.out.println("");

for (int a = 20; a < 30; a++) {
System.out.print(lList.get(a) + " ");
}//end for
System.out.println("");

for (int a = 30; a < 40; a++) {
System.out.print(lList.get(a) + " ");
}//end for
System.out.println("");

for (int a = 40; a < 50; a++) {
System.out.print(lList.get(a) + " ");
}// end for
System.out.println("");

for (int a = 50; a < 60; a++) {
System.out.print(lList.get(a) + " ");
}// end for
System.out.println("");

for (int a = 60; a < 70; a++) {
System.out.print(lList.get(a) + " ");
}// end for
System.out.println("");

for (int a = 70; a < 80; a++) {
System.out.print(lList.get(a) + " ");
}// end for
System.out.println("");

for (int a = 80; a < 90; a++) {
System.out.print(lList.get(a) + " ");
}// end for
System.out.println("");

for (int a = 90; a < 100; a++) {
System.out.print(lList.get(a) + " ");
}// end for

但显然这太多了,而且根本不实用。我已经玩了几个小时试图将其简化为 for 循环或将其更改为最多几行的东西,但一直无法弄清楚。有什么更好的方法来解决这个问题的建议吗?

最佳答案

这个怎么样:

for (int a = 0; a < 100; a++) {
System.out.print(lList.get(a) + " ");
if ((a > 0) && (a % 10) == 0))
System.out.println();
}

关于java - 打印 10x10 表中列表的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22753131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com