【问题标题】:Reverse ArrayList on link链接上的反向 ArrayList
【发布时间】:2020-12-09 12:22:25
【问题描述】:

我有一个 ArrayList,它由称为“历史”的定制对象组成。当我点击一个链接时,我的代码示例顶部名为“Deposit1”的链接,我以正确的顺序显示列表。

但是,当用户单击表内的链接“Deposit2”时,我希望能够反转列表,该链接仅在用户单击第一个链接“Deposit1”后显示。

现在什么都没有发生,但我可以在第二个链接点击时成功 system.out.print。

有没有办法做到这一点?

请注意,为了更好地概述,代码示例已被修剪。

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>

【问题讨论】:

  • if-sortDep2 嵌套在 if-sortDep1 中,因此永远不会被调用为设置 Dep1 或 Dep2 的链接。
  • 是的,正是我的问题。如果我将 if-sortDep2 嵌套在外面,它将“重置”我的页面,并且不显示来自 if-sortDep1 的数据。所以,否则我将不得不做很多冗余。

标签: java jsp arraylist reverse


【解决方案1】:

你可以在你的href标签中为Dept1Dept2传递2参数,这样sortDep1不为空,它会进入if-statement,根据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

【讨论】:

    【解决方案2】:

    将第一个if-sortDep1改为:

    if (sortDep1 != null || sortDep2 != null) {
    

    那么当任何一个有效时,总是输入那部分代码。

    【讨论】:

    • 我试过了。它可以工作,但它会显示列表两次,因为首先在 sortDep1 上调用该列表,然后在显示并单击第二个链接时调用该列表。我想在单击 sortDep2 时来回反转列表 - sortDep2 仅在列表已显示后显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 2016-03-29
    • 2020-07-21
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多