日期:2014-05-20 浏览次数:20935 次
public final class Demo {
private static List<String> nameList=new LinkedList<String>();
/*****************************************************************************
Description : 添加主机名
Input Param : host_name 主机名字符串,非空串
Output Param : 无
Return Value : 成功返回0,失败返回-1
*****************************************************************************/
public static int add_host_name( String host_name)
{
if(host_name!=null && !"".equals(host_name)){
int i=nameList.size()/2;
int min=0;
int max=nameList.size();
String[] inserts=host_name.split("[.]");
out: while(true){
if(i==max){
break out;
}
String[] befores=nameList.get(i).split("[.]");
int x=0;
char[] beforechars=null;
char[] insertchars=null;
//将分隔后的String挨个字符比较,若当前String包含字符前一部分全相同则比较长度,否则继续循环
while(x<Math.min(befores.length, inserts.length)){
String before=befores[befores.length-1-x];
String insert=inserts[inserts.length-1-x];
beforechars=before.toCharArray();
insertchars=insert.toCharArray();
for(int y=0;y<Math.min(beforechars.length, insertchars.length);y++){
if(beforechars[y]==insertchars[y]){
continue;
}else if(beforechars[y]>insertchars[y]){
max=i;
i=(i+1+min)/2;
continue out;
}else if(beforechars[y]<insertchars[y]){
min=i;
i=(i+1+max)/2;
continue out;
}
}
if(beforechars.length>insertchars.length){
max=i;
i=(i+1+min)/2;
continue out;
}else if(beforechars.length<insertchars.length){
min=i;
i=(i+1+max)/2;
continue out;
}
x++;
}
if(befores.length>inserts.length){
max=i;
i=(i+1+min)/2;
continue out;
}else if(befores.length<inserts.length){
min=i;
i=(i+1+max)/2;
continue out;
}
}
nameList.add(i,host_name);
return 0;
}
return -1;
}
/*****************************************************************************
Description : 获取主机名
Input Param : serial_number 排序后的序列号,从1开始
Return Value : 主机名字符串
*****************************************************************************/