public class Main {
public static void main(String sgf[]) throws Exception{
Integer[] data = {1, 2, 3,4,5};
// Integer[] data = new Integer[100];
// for(int i=0;i<100;){
// data[i] = ++i;
// }
System.out.println(fetchBest(data, 2));
}
public static int fetchBest(Integer[] data, int interval) throws Exception{
List list = new ArrayList(Arrays.asList(data));
return fetchBest(list, interval);
}
public static int fetchBest(List<Integer> list, int interval) throws Exception{
if(list == null || interval <= 0){
throw new Exception("参数错误...");
}
if(list.size() == 1){
return list.get(0);
}
int count = 1;
while(list.size()>1){
Iterator it = list.iterator();
while (it.hasNext() && list.size()>1){
it.next();
if(count == interval){
it.remove();
count = 1;
}else {
count++;
}
}
}
return list.get(0);
}
}
public static void main(String sgf[]) throws Exception{
Integer[] data = {1, 2, 3,4,5};
// Integer[] data = new Integer[100];
// for(int i=0;i<100;){
// data[i] = ++i;
// }
System.out.println(fetchBest(data, 2));
}
public static int fetchBest(Integer[] data, int interval) throws Exception{
List list = new ArrayList(Arrays.asList(data));
return fetchBest(list, interval);
}
public static int fetchBest(List<Integer> list, int interval) throws Exception{
if(list == null || interval <= 0){
throw new Exception("参数错误...");
}
if(list.size() == 1){
return list.get(0);
}
int count = 1;
while(list.size()>1){
Iterator it = list.iterator();
while (it.hasNext() && list.size()>1){
it.next();
if(count == interval){
it.remove();
count = 1;
}else {
count++;
}
}
}
return list.get(0);
}
}