我想迭代“座位”类的数组。
public class Seating {
private int nRow, nCol;
private boolean[][] seats;
public Seating () {
nRow = 8;
nCol = 8;
seats = new boolean[nRow][nCol];
}
“Seating”的实例由下面显示的“EventBooking”类创建。
PrintWriter out = response.getWriter();
seats = new Seating(8,8);
seats.setSeatStatus(0,1);
seats.setSeatStatus(1,1);
request.setAttribute("seats", seats.seats);
RequestDispatcher req = request.getRequestDispatcher("/index.jsp");
req.forward(request,response);
和.jsp应该循环遍历这些值。
<c:forEach var = "row" items = "${seats}">
<c:forEach var = "col" items = "${row}">
<c:out value = "${seats}"/>
</c:forEach>
</c:forEach>
我正在尝试将我的“座位”对象发送到.jsp。然后.jsp将遍历Seating的2D数组的值。将打印每个数组的值。
org.apache.jasper.JasperException: An exception occurred processing [/index.jsp] at line [8]
第8行。
8: <c:forEach var = "row" items = "${seats}">
任何帮助表示赞赏。
编辑完整的“座位”代码
public class Seating {
private int nRow, nCol;
public boolean[][] seats;
public Seating () {
nRow = 8;
nCol = 8;
seats = new boolean[nRow][nCol];
}
public Seating (int row, int col) {
nRow = row;
nCol = col;
seats = new boolean[row][col];
}
public boolean seatStatus (int row, int col) {
return seats[row][col];
}
public void setSeatStatus (int row, int col) {
if (seats[row][col] == false) {
seats[row][col] = true;
}
else if (seats[row][col] == true) {
seats[row][col] = false;
}
//else
//Error
}
public int getRowLength () {
return nRow;
}
public int getColLength () {
return nCol;
}
请检查并纠正最里面的循环输出 - Two dimensional arraylist with c:foreach jstl tag
此外,您可以通过索引(例如[1] [0])打印2d数组,以查看它们中是否存在任何值。