Java 链接上的反向数组列表
我有一个ArrayList,它由名为“History”的定制对象组成。当我单击代码示例顶部名为“存款1”的链接时,我会以正确的顺序显示列表 但是,我希望当用户单击表中的链接“Deposit2”时,能够反转列表,该链接仅在用户单击第一个链接“Deposit1”后显示 现在什么也没发生,但我可以通过点击secound链接成功地打印system.out.print 有什么办法可以这样做吗 请注意,为了更好地概述,对代码示例进行了修剪Java 链接上的反向数组列表,java,jsp,arraylist,reverse,Java,Jsp,Arraylist,Reverse,我有一个ArrayList,它由名为“History”的定制对象组成。当我单击代码示例顶部名为“存款1”的链接时,我会以正确的顺序显示列表 但是,我希望当用户单击表中的链接“Deposit2”时,能够反转列表,该链接仅在用户单击第一个链接“Deposit1”后显示 现在什么也没发生,但我可以通过点击secound链接成功地打印system.out.print 有什么办法可以这样做吗 请注意,为了更好地概述,对代码示例进行了修剪 ArrayList <history> history
ArrayList <history> history = historyDAO.getHistory(x, y);
<li>
<a href="history.jsp?Dep1=valueOfDep">Deposit1</a>
</li>
String sortDep1 = request.getParameter("Dep1");
String sortDep2 = request.getParameter("Dep2");
if (sortDep1 != null) {
<table class="table text">
<tr>
<th>Result</th>
<th>Sport</th>
<th>Deposit2<a href="history.jsp?Dep2=valueOfDep">Deposit2</a></th>
</tr>
if (sortDep2 != null) {
Collections.reverse(history);
}
for (int i = 0; i < history.size() && i < list1.size(); i++) {
<tr>
<td><%=history.get(i).getRes()%></td>
<td><%=history.get(i).getSport() %></td>
<td><%=history.get(i).getDeposit()%></td>
</tr>
<%}}%>
</tr>
</table>
arraylisthistory=historyDAO.getHistory(x,y);
字符串sortDep1=request.getParameter(“Dep1”);
字符串sortDep2=request.getParameter(“Dep2”);
如果(sortDep1!=null){
结果
运动
存款2
if(sortDep2!=null){
收藏。反向(历史);
}
对于(int i=0;i
将第一个if-sortDep1
更改为:
if (sortDep1 != null || sortDep2 != null) {
然后,当其中一个有效时,该部分代码总是被输入。您可以在href标记中为
Dept1
&Dept2
传递2
参数,以便sortDep1
不为空,它将进入if语句中,并且根据url中的值,您可以反转列表。例如:
if (sortDep1 != null) {
<table class="table text">
<tr>
<th>Result</th>
<th>Sport</th>
//pass 2 parameter in url for dep1 & dep2
<th>Deposit2<a href="history.jsp?Dep1=valueOfDep&Dep2=reverse">Deposit2</a></th>
</tr>
//check if not null and value is reverse
if ((sortDep2 != null) && (sortDep2.equals("reverse")) {
Collections.reverse(history);
}
//other codes
如果(sortDep1!=null){
结果
运动
//在url中为dep1和dep2传递2参数
存款2
//检查是否不为null,值是否为reverse
如果((sortDep2!=null)&&(sortDep2.equals(“反向”)){
收藏。反向(历史);
}
//其他代码
if-sortDep2嵌套在if-sortDep1中,因此将永远不会被调用,因为链接设置为Dep1或Dep2。是的,这就是我的问题。如果我将if-sortDep2嵌套在外部,它将“重置”我的页面,不显示if-sortDep1中的数据。因此,否则我将不得不进行大量冗余。我已经尝试过。它可以工作,但它将显示列表两次,因为列表首先在sortDep1上调用,然后在第二个链接显示并单击时调用。我想在单击sortDep1时来回反转列表-sortDep2仅在列表已显示后显示。