如何显示2d元素数组

时间:2013-01-29 20:41:32

标签: jsf-2 multidimensional-array

我想使用JSF2在xhtml页面的表格中显示2d浮点数,我不知道该怎么做。我试图在谷歌找到答案,但我不能。所有示例都显示了类对象,我无法使其与表一起使用。

这个引用是: 我有一些大小的数组 - 数组的大小取决于输入的数据: float [] [] variables = new float [size1] [size2]

用户输入数据和pressess按钮后,在托管bean中调用一个方法。计算开始,表格填充数据。

请告诉我如何显示阵列。

1 个答案:

答案 0 :(得分:0)

要实现此目的,您可以使用c:forEach标记来动态地建立h:panelGrid。只需保存size2,即列号作为属性,并将所有输入数字存储在正常java.util.List中。然后将该大小设置为h:panelGrid columns属性,组件将为您分割行。您还可以对c:forEach标记内的内容进行样式设置,并对其进行边界处理,以便为其提供表格行为。

<h:form>
    <h:panelGrid columns="#{bean.numberCount}">
        <c:forEach var="num" items="#{bean.numberList}">
            #{number}
        </c:forEach>
    </h:panelGrid>
</h:form>

<强> EDITED

如果要在列表中维护原始结构,可以创建List<List<Float>>。这意味着List由List组成,其中包含Float对象。与2d数组相同。

private List<List<Float>> _Matrix;

public List<List<Float>> get_Matrix() {
    return this._Matrix;
}

/**
 * Constructor for BackingBean.
 */
public BackingBean() {
    this._Matrix = new ArrayList<List<Float>>();
    this._Matrix.add(new ArrayList<Float>());
    this._Matrix.add(new ArrayList<Float>());
    this._Matrix.get(0).add(1.0f);
    this._Matrix.get(0).add(2.0f);
    this._Matrix.get(0).add(3.0f);
    this._Matrix.get(1).add(1.0f);
    this._Matrix.get(1).add(2.0f);
    this._Matrix.get(1).add(3.0f);
}

在上面的代码中,我构建了一个2d数组,相当于每行2行,值为1.02.03.0。您可以使用此代码在视图上进行迭代:

<h:panelGrid columns="#{backingBean._ColumnNumber}">
        <c:forEach var="row" items="#{backingBean._Matrix}">

            <c:forEach var="value" items="#{row}">

                #{value}
                    </c:forEach>
        </c:forEach>
    </h:panelGrid>

其中#{backingBean._ColumnNumber}将是List的第一个数组的长度(假设它们的长度相同)。

祝你好运。