HiPownedBi
HiPownedBi

Reputation: 195

how to remove the duplicated element in the table JSP JAVA array

I have two arrays:

<%String TXTfileArray[] = (String[])request.getAttribute("txt");%>    
<%String TXTContentArray[] = (String[])request.getAttribute("content");%>

the data in the array is repeated. by using the following statement in JSP i can output the array in the table:

 <table border="1">
   <tr>
    <th>txt name</th>
    <th>txt content</th>
  </tr>
  <% for (int i=0; i< TXTfileArray.length;i++){ %>

  <tr>
    <td>  <%=TXTfileArray[i] %>  </td>    
    <td> <%=TXTContentArray[i] %>  </td>   <%} %> 
   </tr>
   </table>

how to remove the repeated element in the table?

Please do not hard code, as the array is not fixed. the array is depended on the other process.

enter image description here

Upvotes: 0

Views: 608

Answers (1)

Klitos Kyriacou
Klitos Kyriacou

Reputation: 11642

You could try adding each element in a Set, and if it already exists don't add it to the table:

<% Set<String> a = new HashSet<>();
   for (int i=0; i< TXTfileArray.length;i++)
     if (a.add(TXTfileArray[i]) { %>

  <tr>
    <td>  <%=TXTfileArray[i] %>  </td>    
    <td> <%=TXTContentArray[i] %>  </td>   <%} %> 

Although I think JB Nizet's suggestion of using LinkedHashSet<SomeClass> is probably a better way to do it.

Upvotes: 1

Related Questions