too long  no see

解决方案 »

  1.   

    让Person实现Comparable接口.编写自己的compareTo方法,调用Collecitions.sort();方法做升序排序
    或者自己编写一个比较器.同样用Collections.sort(persons,comparator)
      

  2.   

    http://blog.csdn.net/kennylee26/article/details/3926168

    汉字排序
      

  3.   

    你用的HashMap本来就是没有顺序的。你用LinkedHashMap 吧,然后给map的key排序。
      

  4.   


                    case 4:
                     System.out.println("通讯录 \n 输入功能序号:1:单个查询  2:全体查询");
                        Scanner scanner2 = new Scanner(System.in);
                     while(true){
                        int j = scanner2.nextInt();
                        switch (j){
                        case 1: System.out.print("输入要查询的姓名:");
                        PersonUtils.get(scanner2.next());
                        break;
                        case 2:PersonUtils.getAll();
                        break;
                        }
                     }
                    break;
                    
                   我想在switch语句里面嵌套switch语句,可是 添加break跳出外层时提示出错啊????
     
          
      

  5.   

    你算下你的大括号,最后break已经不属于while循环了.
      

  6.   


    import java.util.Comparator;
    import java.util.ArrayList;
    import java.util.Collections;public class Person{
    private String name;
    private String mobile;
    private String email;
    //仅仅添加做实例.需要其他变量可以自己添加 public Person(){
    //default constructor
    } public void setName(String name){
    this.name = name;
    } public void setMobile(String mobile){
    this.mobile = mobile;
    } public void setEmail(String email){
    this.email = email;
    } public String getName(){
    return name;
    } public String getMobile(){
    return mobile;
    } public String getEmail(){
    return email;
    } @Override
    public String toString(){
    return String.format("Person[name:%s,mobile:%s,email:%s]",name,mobile,email);
    } public static void main(String[] args){
    ArrayList<Person> people = new ArrayList<>();
    Person ricky = new Person();
    ricky.setName("ricky"); ricky.setMobile("138888888888");ricky.setEmail("[email protected]");
    Person jennie = new Person();
    jennie.setName("jennie");jennie.setMobile("13999999999");jennie.setEmail("[email protected]");
    people.add(ricky);
    people.add(jennie); PersonComparator comparator = new PersonComparator();
    //默认排序
    Collections.sort(people,comparator);
    for(Person person : people){
    System.out.println(person);
    }
    System.out.println("---------------------------------------");
    //采用MOBILE排序
    Collections.sort(people,comparator.setCompareType(PersonComparator.MOBILE));
    for(Person person : people){
    System.out.println(person);
    }
    } private static class PersonComparator implements Comparator<Person>{
    public int type; static final int NAME = 0;
    static final int MOBILE = 1;
    static final int EMAIL = 2; @Override
    public int compare(Person person1,Person person2){
    if(type == NAME){
    assert person1.getName() != null;
    return person1.getName().compareTo(person2.getName());
    }else if(type == MOBILE){
    assert person1.getMobile() != null;
    return person1.getMobile().compareTo(person2.getMobile());
    }else if(type == EMAIL){
    assert person1.getEmail() != null;
    return person1.getEmail().compareTo(person2.getEmail());
    }

    //默认比较的是名字
    assert person1.getName() != null;
    return person1.getName().compareTo(person2.getName());
    }

    //设置参数可以可以采用不同的排列顺序
    public PersonComparator setCompareType(int type){
    this.type = type;
    return this;
    } }
    }