2014-03-31 57 views
0

我試圖在10 x 10表格中打印LinkedList的100個元素。我有以下代碼:在10x10表格中打印列表元素

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循環,或者將這個變成最多幾行,但一直沒有弄清楚。任何建議爲更好的方式去做這件事?

回答

4

如何:

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

這一工程!唯一的是,它在第​​一行打印了11個項目,最後打印了9個項目。 – Luminusss

+0

@xTopShelfx希望你能解決這個問題;-)請接受它的幫助! –

+0

這絕對有幫助,我只是想搞清楚如何改變最後一排! – Luminusss