反转链表上的ArrayList

huangapple go评论59阅读模式
英文:

Reverse ArrayList on link

问题

我有一个 ArrayList,其中包含名为 "History" 的自定义对象。当我点击代码示例顶部的名为 "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>
英文:

I have an ArrayList which consist of custom made objects called "History". When I click a link, the one called "Deposit1" at the top of my code example, I display the list in correct order.

However, I want to be able to reverse the list when the user click on the link "Deposit2" inside the table which is only displayed after the users click on the first link, "Deposit1".

Right now nothing happends, but I can system.out.print succesfully on secound link click.

Is there anyway to do this?

Note that code example is trimmed for better overview.

ArrayList &lt;history&gt; history = historyDAO.getHistory(x, y);



&lt;li&gt;
     &lt;a href=&quot;history.jsp?Dep1=valueOfDep&quot;&gt;Deposit1&lt;/a&gt;
&lt;/li&gt;

String sortDep1 = request.getParameter(&quot;Dep1&quot;); 
String sortDep2 = request.getParameter(&quot;Dep2&quot;);

if (sortDep1 != null) {         
    
    &lt;table class=&quot;table text&quot;&gt;
      &lt;tr&gt;
        &lt;th&gt;Result&lt;/th&gt;
		&lt;th&gt;Sport&lt;/th&gt;
        &lt;th&gt;Deposit2&lt;a href=&quot;history.jsp?Dep2=valueOfDep&quot;&gt;Deposit2&lt;/a&gt;&lt;/th&gt;
      &lt;/tr&gt;

 if (sortDep2 != null) {
    Collections.reverse(history);
   }   


for (int i = 0; i &lt; history.size() &amp;&amp; i &lt; list1.size(); i++) {
     
    &lt;tr&gt;
            &lt;td&gt;&lt;%=history.get(i).getRes()%&gt;&lt;/td&gt;
			&lt;td&gt;&lt;%=history.get(i).getSport() %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%=history.get(i).getDeposit()%&gt;&lt;/td&gt;
    &lt;/tr&gt;
  
&lt;%}}%&gt;

&lt;/tr&gt;
&lt;/table&gt;

答案1

得分: 1

您可以在 href 标签中传递 `2` 个参数,分别用于 `Dept1` 和 `Dept2`,以便 `sortDep1` 不为空,这样它将进入 `if` 语句,并根据 URL 中的值对列表进行反转,例如:

    if (sortDep1 != null) {
        &lt;table class=&quot;table text&quot;&gt;
        &lt;tr&gt;
           &lt;th&gt;结果&lt;/th&gt;
           &lt;th&gt;体育项目&lt;/th&gt;
           // 在 URL 中传递 2 个参数,用于 dep1 和 dep2
           &lt;th&gt;存款2&lt;a href=&quot;history.jsp?Dep1=valueOfDep&amp;Dep2=reverse&quot;&gt;存款2&lt;/a&gt;&lt;/th&gt;
        &lt;/tr&gt;
        // 检查不为 null 并且值为 reverse
        if ((sortDep2 != null) &amp;&amp; (sortDep2.equals(&quot;reverse&quot;))) {
            Collections.reverse(history);
        }   
        // 其他代码
    }
英文:

You can pass 2 parameters in your href tag for both Dept1 & Dept2 so that sortDep1 is not null and it will go inside if-statement and depending on the value which is in url you can reverse your list. i.e :

if (sortDep1 != null) {         
&lt;table class=&quot;table text&quot;&gt;
&lt;tr&gt;
   &lt;th&gt;Result&lt;/th&gt;
   &lt;th&gt;Sport&lt;/th&gt;
   //pass 2 parameter in url for dep1 &amp; dep2
   &lt;th&gt;Deposit2&lt;a href=&quot;history.jsp?Dep1=valueOfDep&amp;Dep2=reverse&quot;&gt;Deposit2&lt;/a&gt;&lt;/th&gt;
&lt;/tr&gt;
//check if not null and value is reverse
  if ((sortDep2 != null) &amp;&amp; (sortDep2.equals(&quot;reverse&quot;)) {
    Collections.reverse(history);
    }   
 //other codes

答案2

得分: 0

将第一个if-sortDep1更改为:

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

然后当任一条件有效时,该代码部分将始终被执行。

英文:

Change the first if-sortDep1 to:

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

then that part of code is always entered when either is valid.

huangapple
  • 本文由 发表于 2020年8月20日 15:38:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63500419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定