首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java的Comparable不支持中文吗,该怎么处理

2012-01-21 
java的Comparable不支持中文吗Java codeimport java.util.ArrayListimport java.util.Collectionspublic

java的Comparable不支持中文吗

Java code
import java.util.ArrayList;import java.util.Collections;public class StudentRanging {    public static void main(String[] args) {        ArrayList<Student> list = new ArrayList<Student>();        Student a = new Student("胖胖", 30, 90.5);        Student b = new Student("刘静", 25, 92.5);        Student c = new Student("苏凯多多", 21, 80.5);        Student f = new Student("阿成才", 24, 80.5);        Student d = new Student("许三多", 28, 90.5);        Student e = new Student("成才", 20, 90.5);        list.add(a);        list.add(b);        list.add(c);        list.add(d);        list.add(e);        list.add(f);        Collections.sort(list);        for (Student s : list) {            System.out.println("info:" + s);        }    }    public static class Student implements Comparable<Student> {        String name;        int age;        double score;        public Student(String name, int age, double score) {            this.name = name;            this.age = age;            this.score = score;        }        public String getName() {            return name;        }        public int getAge() {            return age;        }        public double getScore() {            return score;        }        public void setName(String name) {            this.name = name;        }        public void setAge(int age) {            this.age = age;        }        public void setScore(double score) {            this.score = score;        }        @Override        public String toString() {            return name + " " + age + " " + score;        }        @Override        public int compareTo(Student o) {            int result = 0;            return this.getName().compareTo(o.getName());        }    }}


[解决办法]
这是根据汉字的unicode值来排序的。你要按照汉字拼音的首字母来排序:
比如:阿成才,成才,刘静,胖胖,苏凯多多,许三多
那复杂些看这篇博客

热点排行
Bad Request.