0

だから私はこれを理解しようとしています。名前の配列リストを年齢で並べ替え、次に名前で並べ替える必要があります (年齢が等しい場合)。これを行う簡単な方法があると確信していますが、レッスンではインターフェイスを使用する必要があります。これまでのところ、人の名前と年齢の配列リストと、情報を取得できるメソッドでいっぱいの person クラスがあります。リストをソートしてメインクラスに戻すにはどうすればよいですか? パーソンソーター:

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;
    }

}

人々:

これは、配列リストを取得し、必要に応じて年齢、名前でソートするクラスです。

4

4 に答える 4

5

あなたはほとんどの仕事をしました。Personクラスに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);
}

メソッドCollections.sortは、 compareToメソッドによって提供される順序に従って項目を並べ替えます。

于 2013-04-07T16:32:57.893 に答える
0

複数の組み合わせ Comparator を作成し続けたくない場合は、Group Comparatorのようなものを使用できます。

于 2013-04-07T17:11:29.607 に答える
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); 
于 2013-04-07T16:58:33.897 に答える
0

Google の Guava は本当にきちんとしComparatorChainた .

于 2013-04-07T17:16:06.083 に答える