Xavier
Xavier

Reputation: 399

Repeat accessing array items every second in Java

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() {

    }
};

Upvotes: 0

Views: 156

Answers (1)

icrovett
icrovett

Reputation: 437

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...

Upvotes: 1

Related Questions