English 中文(简体)
从阵列清单中编制3栏.html表?
原标题:Creating a 3 column html table from arraylist?
  • 时间:2012-04-18 05:53:43
  •  标签:
  • java
  • html
  • jsp

我如何能够从上以超文本形式设立3栏表格?

我目前的法典就是这样:

<table border="0">

        <%
            for (int i = 1; i < states.size(); i++) {
        %>
        <TR>
            <%
                for (int col = 1; col <= 3; col++) {
            %>
            <TD>
                <%
                    out.println(states.get(i));}
                %>
            </TD>
            
        </TR>
        <%
            }
        %>
    </table>

I get a 3 column format table but 3x the same entry in each row...

Expected output

Albania | Algeria   | American Samoa
Andorra | Angola    | Anguilla
..

我失踪了什么?

最佳回答

如果我正确理解这一要求(但不能肯定是这种情况),那么,这样就能使你开始。

<table border="0">
    <tr>
    <%
        for (int i = 1; i < states.size(); i++) {
            out.println("<td>" + states.get(i) + "</td>");
            if (i>0 && i%3==0) {
                out.println("</tr><tr>");
            }
        }
    %>
    </tr>
</table>

它将按照这些思路产生产出。

Albania | Algeria   | American Samoa
Andorra | Angola    | Anguilla
..

注:它仍有问题。 如果3个国家有多个,表末将有一个完全空洞的线。 如果不是的话,最后一行将不会有正确的栏号。 BNI。

问题回答

It s just a small typo. You re indexing with i , but your internal variable is col.

修改如下:

...
out.println(states.get(3*col + i));
...

如果是这样,你现在可能需要将一栏索引为零。

注:

  • Index in List start with 0
  • 你们在找回之前需要计算该指数。

        <%
            for (int i = 0; i < states.size(); i++) {
        %>
        <TR>
            <%
                for (int col = 0; col < 3; col++) {
            %>
            <TD>
                <%  
                    int index= (i*3)+col;
                    if(states.size()>index+1)
                    {    
                     out.println(states.get(index));
                    }
                    i = i+1;  
                %>
            </TD>
    
        </TR>
        <%
            }
        %>
    </table>
    

请尝试这样做。 仅增加另一个变量,在从您的阵列清单中检索胎儿时将使用这些变量。

<table border="0">

    <%

        for (int i = 1, index = 0; i < states.size(); i++) {
    %>
    <TR>
        <%
            for (int col = 1; col <= 3; col++) {
        %>
        <TD>
            <%
                out.println(states.get(index++));}
            %>
        </TD>

    </TR>
    <%
        }
    %>
</table>




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签