JSP中的String Array(String [] [])输出

时间:2016-03-17 15:47:42

标签: java arrays string jsp printing

我对JSP完全陌生。我想要的是基本上用JSP打印一个String [] [],就像我在eclipse中在控制台上做的那样:

private class Itr implements Iterator<E> {
    int cursor;       // index of next element to return
    int lastRet = -1; // index of last element returned; -1 if no such
    int expectedModCount = modCount;

    public boolean hasNext() {
        return cursor != size;
    }

    @SuppressWarnings("unchecked")
    public E next() {
        checkForComodification();
        int i = cursor;
        if (i >= size)
            throw new NoSuchElementException();
        Object[] elementData = ArrayList.this.elementData;
        if (i >= elementData.length)
            throw new ConcurrentModificationException();
        cursor = i + 1;
        return (E) elementData[lastRet = i];
    }

    public void remove() {
        if (lastRet < 0)
            throw new IllegalStateException();
        checkForComodification();

        try {
            ArrayList.this.remove(lastRet);
            cursor = lastRet;
            lastRet = -1;
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    }

    final void checkForComodification() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        }
    }

它想要这样:

public void printField(String[][] field){
        for (int i = 0; i < field.length-1; i++){
            for (int j = 0; j < field[i].length-1; j++){
                String x = field[j][i];
                System.out.print("|");

                if (x.equals(" ")){
                    System.out.print("   ");
                } else if (x.equals("S")) {
                    System.out.print(" S ");
                } else if (x.equals("x")) {
                    System.out.print(" x ");
                } else if (x.equals("#")){
                    System.out.print(" # ");
                } else if (x.equals("*")){
                    System.out.print(" * ");
                }
                else if (x.equals("10")){
                    System.out.print(" "+x );
                }
                else {
                    System.out.print(" " +x +" ");
                }
            }
            System.out.println("|");
        }
        System.out.println();
}

正如一般信息:我已编码战舰。我现在想做的是现在创建一个基于网络的前端,这就是我要问的原因。我将在稍后用图形项替换此字段输出。但它只是出于学习目的。

1 个答案:

答案 0 :(得分:0)

首先。你是否意识到所有这些条件都是完全没必要的?

if (x.equals(" ")){
    System.out.print("   ");
} else if (x.equals("S")) {
    System.out.print(" S ");
} else if (x.equals("x")) {
    System.out.print(" x ");
} // others....

你取值并先添加一个空格然后......然后你的代码与:

相同
if (x.equals("10")){
    System.out.print(" "+x );
} else {
    System.out.print(" " + x + " ");
}

你也可以使用String.format ...但无论如何......

在JSP中显示此结构:

在请求中设置您的属性

request.setAttribute("field", field);

然后在JSP中:

<table class="your_class">
    <thead>
        not necessary
    </thead>

然后使用:

迭代元素
    <tbody>
        <!-- each item in field will be an 1d array -->
        <c:forEach items="${field}" var="row">
            <td> |
                <tr class="your_class">
                <!-- each item in row will be an element -->
                <c:forEach items="${row}" var="item">
                    ${item} |
                </c:forEach>
                </tr>
            </td>
        </c:forEach>
    </tbody>
</table>