0

I am very new in Java. Say I have a ArrayList with 10 string items, what I am trying to archive are

  1. Print each items every second.

  2. When all items are printed, it will return to the beginning, repeating printing

Can you give me some ideas in Java

ArrayList<String> testAL = new ArrayList<String>();
Timer tickerTimer = new Timer();
TimerTask sendMessageTask = new TimerTask() {
    public void run() {

    }
};
Deepak Bala
  • 11,095
  • 2
  • 38
  • 49
Xavier
  • 389
  • 7
  • 20

1 Answers1

1

You can try something like:

try {
   for (int i = 0; i < testAL.size(); i++) {
     System.out.println(testAL.get(i);
     Thread.sleep(1000);
   }
} catch (InterruptedException e) {
   e.printStackTrace();
}

And run your TimerTask from the main... Also you got a Timer that can execute the TimerTask run every X seconds...

icrovett
  • 435
  • 7
  • 21
  • Thanks. I tried this way. But printing did not repeat, any suggestion? – Xavier Apr 10 '13 at 13:54
  • You need to loop over the execution of the run method the simple way: `for(;;)sendMessageTask.run();` but you need to work in the solution and improve it ;) – icrovett Apr 10 '13 at 13:57