根据固定的列表,依次轮循
public class RoundRobin {
/**
* 轮询角标
*/
private int indexNum;
/**
* 待轮循返回的List集合
*/
private final List<String> stringList;
/**
* 轮循返回的List集合长度
*/
private final int stringListSize;
/**
* 获取下一个
* @return
*/
public String getNext(){
List<String> strings=this.stringList;
if(strings==null || strings.size() == 0){
return null;
}
if(indexNum % stringListSize==0){
indexNum=0;
}
String str=strings.get(indexNum);
indexNum++;
return str;
}
public RoundRobin(List<String> stringList) {
this.stringList = stringList;
this.stringListSize=stringList==null ? 0 : stringList.size();
}
}
这里是测试类
@Test
public void Test() {
String[] str={"1","2","3","4","5","6"};
List<String> strings=Arrays.asList(str);
RoundRobin roll=new RoundRobin();
roll.setStringList(strings);
for (int i = 0; i < 101;i++) {
String s=roll.getNext();
System.out.println(s);
}
}