如何根据时间间隔检索ArrayList的价值? [英] How to retrieve value from arraylist according to time interval?
问题描述
你好我想获取一个网址,当我获取该网址我得到的字符串。我一直在我的 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 = "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) {
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的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!