刚才写错了,没有用容器- public static void main(String[] args) {
- final int index = 10;
- final String[] strs = new String[10];
- for (int i = 0; i < index; i++) {
- strs[i] = i + " data ";
- }
- final ArrayList<String> al=new ArrayList<String>();
- final Timer timer = new Timer();
- TimerTask task=new TimerTask() {
- int i=0;
- public void run() {
- for (int j = 0; j < 2; j++) {
- System.out.println(strs[i]);
- al.add(strs[i]);
- i++;
- if(i==index)
- timer.cancel();
- }
- System.out.println("--------------");
- }
- };
- timer.scheduleAtFixedRate(task, 0, 60);
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Iterator<String> als=al.iterator();
- while(als.hasNext()){
- System.out.println(als.next());
- }
- }
复制代码 |