JAVA姓名工资排序,菜鸟新手问题
import java.io.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
List c = new ArrayList();
String save;
String[] temp;
try {
fr = new FileReader("C:\\test\\source.txt"); // 读文件
BufferedReader br = new BufferedReader(fr);
while ((save = br.readLine()) != null) { // 按行读
temp = save.split(" "); // 用空格分开
if(temp.length == 2)
c.add(new Person(temp[0], Integer.parseInt(temp[1]))); // 分成 字符型 和 整型
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("文件打开失败!");
System.exit(-1);
} catch (IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}
Collections.sort(c); // 排序
try {
fw = new FileWriter("C:\\test\\result.txt"); // 写入路径
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < c.size() - 1; i++) {
String fin = c.get(i) + "\n"; // 以换行符为标准写入
bw.write(fin);
System.out.print(fin);
}
bw.close();
} catch (IOException e) {
System.out.println("文件写入错误!");
System.exit(-1);
}
}
}
class Person implements Comparable { // 1.有问题 Comparable接口
public String name;
public Integer salary;
public Person(String name, Integer salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public Integer getSalary() {
return salary;
}
/*
public int compareTo(Person o) {
int re = this.salary > o.getSalary() ? 1
: (this.salary < o.getSalary() ? -1 : 0);
return re;
} */
public int compareTo(Object o) {
Person n = (Person)o;
return 0;
}
}
/*source.txt
*
小王 10000
小强 2345
小张 2342
蜡笔小新蜡笔小新 2000
小强 1030
小周 1020
Tom 3000
Jerry 1500
justin 1500
jerry 1500
Abel 1500
中国 1200
张三 2000
李宗盛 2000
王二 2000
刘德华 2000
*/
想问下compareTo这个应该怎么写,我写的注释的那段是什么问题(按照工资排序,姓名按照首字母排序),或者还有点其他的问题
我的异常网推荐解决方案:软件开发者薪资,http://www.myexception.cn/other/1391128.html