mport java.io.*;
import java.util.*;
public class Test5 {
public static void main(String [] args)throws Exception{
writerTo();
show();
}
public static void writerTo()throws Exception{
FileWriter fw=new FileWriter("Hi.txt");
fw.write("61.54.231.245\r\n");
fw.write("61.54.231.9\r\n");
fw.write("61.54.231.246\r\n");
fw.write("61.53.231.249\r\n");
//fw.flush();
fw.close();
}
public static void show()throws Exception{
BufferedReader bufr=new BufferedReader(new FileReader("Hi.txt"));
String line=null;
Set<IPAr> stree=new TreeSet<IPAr>();
int x=0;
while((line=bufr.readLine())!=null){
String s=line;
stree.add(new IPAr(line));
}
bufr.close();
for(IPAr a:stree){
System.out.println(a);
}
}
}
class IPAr implements Comparable<IPAr>{///问题就在这里,我明明覆盖了compareTo方法,为什么命令行总是说没覆盖呢,
private String ip;
IPAr(String ip){
this.ip=ip;
}
public int compareTo(Object obj){
if(!(obj instanceof IPAr))
throw new RuntimeException("地址错误");
IPAr a=(IPAr)obj;
String[] sa=a.ip.split(".");
String[] s=this.ip.split(".");
for(int x=0;x<s.length;x++){
int y=Integer.parseInt(s[x])-Integer.parseInt(sa[x]);
if(y!=0)
return y;
}
return 0;
}
}
|
|