简体   繁体   中英

How to get value from arraylist?

I am trying to fetch an url -where I am getting- and some values whohse I stored into two variables. I put those variables into an arrlist. Now I want to print out that array list on my jsp page. The restriction: I want print out tghe first 50 values at once and after some seconds seconds the next 100 values. But previous values should not display then next 50 value should display and privous not display at the end from starting value should display.

Here is my jsp code

  <div class="push">
  <table  width="100%" border="1" align="center" cellpadding="0" cellspacing="1" bordercolor='66A8FF'>
     <%

       URL url;
        try {
            // get URL content

            String a="http://122.160.81.37:8080/mandic/commoditywise?c=paddy";
            url = new URL(a);
            URLConnection conn = url.openConnection();

            // open the stream and put it into BufferedReader
            BufferedReader br = new BufferedReader(
                               new InputStreamReader(conn.getInputStream()));
StringBuffer sb=new StringBuffer();
            String inputLine;
            ArrayList<String> list1=new ArrayList<String>();
                  ArrayList<String> list2=new ArrayList<String>();
            while ((inputLine = br.readLine()) != null) {
                    System.out.println(inputLine);

                   String s=inputLine.replace("|", "\n");

                    s=s.replace("~"," ");

                    StringTokenizer str = new StringTokenizer(s);
                    while(str.hasMoreTokens())

   {
       String mandi = str.nextElement().toString();

          String price = str.nextElement().toString();
           list1.add(mandi);
         list2.add(price);
         }
            }
     %>

            <%

                String item1 = null;
            int i=0;
              int j=0;
            for ( i= 0; i < list1.size(); i++) 
            {
                %>
          <tr bgcolor="0F57FF" style="border-collapse:collapse">
                  <td  width="50%"  height="50px" align="center" style="font-size:24px"><font color="#fff"><%= list1.get(i)%></font></td>   
         <%
          for ( j = 0; j < list2.size(); j++)  
         %>
             <td  width="50%"  height="50px" align="center" style="font-size:24px"><font color="#fff"><%= list2.get(j)%></font></td>
            </tr>
                  <%

            }
              br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }    

       %>
    </table>
    </div>

How can I achieve my output?

Thanks in advance

You have to store your list in session and than refresh your jsp for some interval. every time you will have two lists one which is in session and second is your current fetched list.

now, if you want to remove common (display newly added) elements from arraylist you have to do this.

1.make a union of the two arrays (new and old)

2.make the intersection out of them

3.Subtract the intersection from the union to get your result

// suppose you have two list as below
List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
List<Integer> list2 = Arrays.asList(2, 3, 4, 6, 7);
// Prepare a union
List<Integer> union = new ArrayList<Integer>(list1);
union.addAll(list2);
// Prepare an intersection
List<Integer> intersection = new ArrayList<Integer>(list1);
intersection.retainAll(list2);
// Subtract the intersection from the union
union.removeAll(intersection);
// Print the result
for (Integer n : union) {
    System.out.println(n);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM