根据取模选择不同的列表
public class test1122
{
/**
* 根据取模选择不同的列表(保证code结尾的数字总是配置同一样的列表)
* 请观察code与value的映射关系(规律:通过取模匹配)
* 源需求:请保证以code结尾的号码总是匹配用一个list成员之一(列表)
* @param args
*/
public static void main(String[] args)
{
int[] code = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
String[] list =
{"A:192.168.66.16,192.168.66.17,192.168.66.18",
"B:192.168.66.16,192.168.66.17,192.168.66.18",
"C:192.168.66.18,192.168.66.17,192.168.66.16",
"D:192.168.66.18,192.168.66.17,192.168.66.16"};
int size = list.length;
for (int i = 0; i < code.length; i++)
{
int index = code[i] % size;
System.out.println("code="+code[i]+", size="+size+", index=" + index + ", value=" + list[index]);
}
}
}
运行结果:
code=0, size=4, index=0, value=A:192.168.66.16,192.168.66.17,192.168.66.18
code=1, size=4, index=1, value=B:192.168.66.16,192.168.66.17,192.168.66.18
code=2, size=4, index=2, value=C:192.168.66.18,192.168.66.17,192.168.66.16
code=3, size=4, index=3, value=D:192.168.66.18,192.168.66.17,192.168.66.16
code=4, size=4, index=0, value=A:192.168.66.16,192.168.66.17,192.168.66.18
code=5, size=4, index=1, value=B:192.168.66.16,192.168.66.17,192.168.66.18
code=6, size=4, index=2, value=C:192.168.66.18,192.168.66.17,192.168.66.16
code=7, size=4, index=3, value=D:192.168.66.18,192.168.66.17,192.168.66.16
code=8, size=4, index=0, value=A:192.168.66.16,192.168.66.17,192.168.66.18
code=9, size=4, index=1, value=B:192.168.66.16,192.168.66.17,192.168.66.18