2013-04-07 52 views
0

所以我想弄明白這一點。我製作了一個年齡名稱的數組列表,然後按名稱排序(如果年齡相同),我確信有一個簡單的方法可以做到這一點,但我們的教訓是要求我們使用接口。所以我到目前爲止是一個人名和年齡的數組列表,然後是一個充滿了我可以從中檢索信息的方法的人類。我如何對列表進行排序以傳回到我的主類中? PersonSorter:如何按2約束進行排序?

import java.util.ArrayList; 
import java.util.Collections; 


public class PersonSorter 
{ 
    public static void main(String[] args) 
    { 
     ArrayList<Person> people = new ArrayList<Person>(); 

     people.add(new Person("Linda", 63)); 
     people.add(new Person("Jacob", 5)); 
     people.add(new Person("Emily", 13)); 
     people.add(new Person("Jessica", 21)); 
     people.add(new Person("Emma", 5)); 
     people.add(new Person("Robert", 80)); 
     people.add(new Person("Jennifer", 43)); 

     // PRINT THE LIST OF PEOPLE BEFORE SORTING 
     for (Person person : people) 
     { 
      System.out.println(person); 
     } 

     System.out.println();//space between the lists 

     Collections.sort(people); 

     // PRINT THE LIST OF PEOPLE AFTER SORTING 
     for (Person person : people) 
     { 
      System.out.println(person); 
     } 
    } 
} 

人:

public class Person implements Comparable<Person> 
{ 
    /** The person's name */ 
    private int age; 

    /** The person's age */ 
    private String name; 

    /** 
    * Constructs a new Person object with the given name and age 
    * 
    * @param age of the person 
    * @param name of the person 
    */ 
    public Person(String name, int age) 
    { 
     this.age = age; 
     this.name = name; 
    } 


    /** 
    * 
    * Returns the age of the person 
    * 
    * @return age 
    */ 
    public int getAge() 
    { 
     return age; 
    } 


    /** 
    * 
    * Sets the age of the person 
    * 
    * @param age 
    */ 
    public void setAge(int age) 
    { 
     this.age = age; 
    } 


    /** 
    * Returns the name of the person 
    * 
    * @return name 
    */ 
    public String getName() 
    { 
     return name; 
    } 


    /** 
    * 
    * Sets the name of the person 
    * 
    * @param name 
    */ 
    public void setName(String name) 
    { 
     this.name = name; 
    } 



    @Override 
    /** 
    * Returns a string representation of the person in the form: 
    * Person[name = [name], age = [age]] 
    */ 
    public String toString() 
    { 
     return "Person [name=" + name + ", age=" + age + "]"; 
    } 


    /* (non-Javadoc) 
    * @see java.lang.Comparable#compareTo(java.lang.Object) 
    */ 
    @Override 
    public int compareTo(Person o) 
    { 

     return 0; 
    } 

} 

人:

這是我將拉動數組列表和按年齡排序類,然後名稱,如果需要的話。

回答

5

你已經完成了大部分工作。只實現的compareTo類:

@Override 
public int compareTo(Person o) { 
    if (this.age != o.age) { 
     return this.age < o.age ? -1 : 1; 
    } 
    return this.name.compareTo(o.name); 
} 

方法根據的compareTo方法提供的順序Collections.sort各種各樣的物品。

+0

OMG就這麼簡單!在這裏,我無法越過我寫的代碼,認爲我做錯了什麼。 Thanx Maroun和Grzegorz,你這樣的親愛的! – 2013-04-07 16:37:37

0
public int compareTo(Person p) { 

     return this.age - p.age; 

    } 

    public static Comparator<Person> PersonComparator = new Comparator<Person>() { 

     public int compare(Person p1, Person p2) { 

      String firstPerson = p1.name; 
      String secondPerson = p2.name; 

      return firstPerson.compareTo(secondPerson); 

     } 

    }; 

將此代碼添加到您的Person類:

如果要排序的年齡嘗試:

Arrays.sort(people); 

如果要排序的名字嘗試:

Arrays.sort(people, Person.PersonComparator);