如何根据时间间隔检索ArrayList的价值?

编程入门 行业动态 更新时间:2024-10-27 12:41:20
本文介绍了如何根据时间间隔检索ArrayList的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好我想获取一个网址,当我获取该网址我得到的字符串。我一直在我的 ArrayList中的字符串。现在,我想打印的ArrayList 值。条件是,只有15个值应打印每x秒。

Hi I am trying to fetch a URL and when I fetch that URL I get a string. The string I kept in my arraylist. Now I want to print arraylist value. The condition is that only 15 values should print every x seconds.

下面是我的code:

public class CommoditywiseGetUrl { public static void main(String args[]) { URL url; try { // get URL content String a = "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) { String s = inputLine.replace("|", "\n"); s = s.replace("~", " "); //System.out.println(s); 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; for (int i = 0; i < list1.size(); i++) { item1 = list1.get(i); System.out.println("mandi" + item1); } for (int i = 0; i < list2.size(); i++) { String item2 = list2.get(i); System.out.println("Price" + item2); } br.close(); //System.out.println(sb); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

我怎样才能达到我期望的输出?

How can I reach my desired output?

感谢。

推荐答案

您列表填充的逻辑是不相关的列表元素的预定打印。所以,最好不要混合使用它们。我建议你​​用java util的计时器来调度细粒度控制打印。我不能够运行您的code,但在这里是如何使用定时器一个想法:

Your list population logic is not related to the scheduled printing of the list elements. So better not mix them. I would recommend you use java util Timer to schedule the printing for fine grained control. I am not able to run your code, but here is an idea on how you can use timer:

Timer timer = new Timer(); timer.schedule(new RunMeTask(), 1000,60000); // executes every 60 secs with 1 sec delay

而RumeMetask可能会像

And the RumeMetask would probably look like

public static class RunMeTask extends TimerTask { @Override public void run() { String item1 = null; for (int i = count; i < 16; i++) { item1 = list1.get(i); System.out.println("mandi" + item1); } for (int i = count; i < 16; i++) { String item2 = list2.get(i); System.out.println("Price" + item2); } } }

更多推荐

如何根据时间间隔检索ArrayList的价值?

本文发布于:2023-10-16 11:25:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   价值   时间   ArrayList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!