java 将 arraylist 转换为 html 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10597072/
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
convert arraylist to html table
提问by Gipsy
how do i retrieve my arraylist into html table?
如何将我的数组列表检索到 html 表中?
my arraylist:
我的数组列表:
[
1, 2011-05-10, 1, 22.0,
2, 2011-05-10, 2, 5555.0,
3, 2011-05-11, 3, 123.0,
4, 2011-05-11, 2, 212.0,
5, 2011-05-30, 1, 3000.0,
6, 2011-05-30, 1, 30.0,
7, 2011-06-06, 1, 307.0,
8, 2011-06-06, 1, 307.0 ]
out.println("<html>");
out.println("<head>");
out.println("<title>Counter</title>");
out.println("</head>");
out.println("<body>");
out.println("<table border=\"1\">");
out.println("<tr>");
for (int i = 0; i < ex.getExpenses().size(); i++) {
out.println("<td>" + ex.getExpenses().get(i) + "</td>");
if (i>0 && i%4==0) {
out.println("</tr><tr>");
}
}
out.println("</tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
this doesn't really work, since arraylist starts with 0, it gives me the table that looks like this:
这实际上不起作用,因为 arraylist 从 0 开始,它给了我看起来像这样的表:
how do i get the table that looks like this:
我如何获得看起来像这样的表格:
1 2011-05-10 1 22.0
2 2011-05-10 2 5555.0
3 2011-05-11 3 123.0
4 2011-05-11 2 212.0
5 2011-05-30 1 3000.0
6 2011-05-30 1 30.0
7 2011-06-06 1 307.0
8 2011-06-06 1 307.0
回答by Mikita Belahlazau
Replace
代替
for (int i = 0; i < ex.getExpenses().size(); i++) {
out.println("<td>" + ex.getExpenses().get(i) + "</td>");
if (i>0 && i%4==0) {
out.println("</tr><tr>");
}
}
with
和
for (int i = 0; i < ex.getExpenses().size(); i++) {
if (i>0 && i%4==0) {
out.println("</tr><tr>");
}
out.println("<td>" + ex.getExpenses().get(i) + "</td>");
}
By the way. Create separate class Expenses
that contains 4 fields. It will be much cleaner and easier to convert this to html. You can write special separate method for converting Expenses
to html table row.
顺便一提。创建Expenses
包含 4 个字段的单独类。将其转换为 html 会更清晰、更容易。您可以编写特殊的单独方法来转换Expenses
为 html 表行。
回答by Alexander Rafferty
Change the i
to (i+1)
:
更改i
为(i+1)
:
[
1, 2011-05-10, 1, 22.0,
2, 2011-05-10, 2, 5555.0,
3, 2011-05-11, 3, 123.0,
4, 2011-05-11, 2, 212.0,
5, 2011-05-30, 1, 3000.0,
6, 2011-05-30, 1, 30.0,
7, 2011-06-06, 1, 307.0,
8, 2011-06-06, 1, 307.0 ]
out.println("<html>");
out.println("<head>");
out.println("<title>Counter</title>");
out.println("</head>");
out.println("<body>");
out.println("<table border=\"1\">");
out.println("<tr>");
for (int i = 0; i < ex.getExpenses().size(); i++) {
out.println("<td>" + ex.getExpenses().get(i) + "</td>");
if (i>0 && (i+1)%4==0) {
out.println("</tr><tr>");
}
}
out.println("</tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
回答by Kousalik
first thing: Get arrylist into separate variable and then itarate it with foreach, instead of calling get() and get(i) repeatedly, then change your if condition
第一件事:将 arrylist 放入单独的变量中,然后使用 foreach 对其进行迭代,而不是重复调用 get() 和 get(i),然后更改您的 if 条件
if ((i+1)%4==0) {
out.println("</tr><tr>");
}
second thing:
Use StringBuilder
class to concatenate output string, then print it with only one out.print
第二件事:使用StringBuilder
类连接输出字符串,然后只打印一个out.print