实现一个固定长度的集合,每个1s中刷新一次,第一个元素去掉,从最后面添加一个元素。

来源:互联网 发布:淘宝外国模特 编辑:程序博客网 时间:2024/06/05 14:38
import java.util.Timer;

/*
 * 需求:实现一个固定长度的集合,每个1s中刷新一次,第一个元素去掉,从最后面添加一个元素。
 */
public class FixedLength {


public static void main(String[] args) {


Timer t = new Timer();
t.schedule(new MyTask(MyTask.num), 1000, 1000);

}

}



import java.util.ArrayList;
import java.util.TimerTask;


public class MyTask extends TimerTask{
public static int num=0;
MyTask(int n) {
this.num = n;
}

public void run(){
ArrayList<Integer> array = new ArrayList<Integer>();
array = newArray();

int count = this.num;  //集合的开始


for(int j=count; j<count+1; j++){
ArrayList<Integer> arrs = fixedArray(20,j, array);
System.out.println("arrs:" + arrs);
}
this.num ++;
}


/**
* 实时产生数据集合
* @return
*/
public static ArrayList<Integer> fixedArray(int n, int counts, ArrayList<Integer> arrays){
ArrayList<Integer> array = new ArrayList<Integer>();

int count = counts;
for(int i=count; i<n+count; i++){
array.add(arrays.get(i));
}
count ++;

return array;
}


/**
* 获取序号的集合
* @param n : 集合的长度
* @param counts : 计数开始
* @param arrays : 总的集合
* @return :需要的集合
*/
public static ArrayList<Integer> newArray(){
ArrayList<Integer> array = new ArrayList<Integer>();
for(int i=0; i<1000; i++){
array.add(i);
}
return array;
}

}



0 0
原创粉丝点击