1

我已经尝试了几种方法,第一种是在以下代码的底部有一个实现比较器的类。当我尝试在 sortListByLastName 中传递比较时,我得到一个未找到构造函数的错误,我不知道为什么

import java.util.*;

public class OrganizeThis implements WhoDoneIt
{
    /** 
    Add a person to the organizer

    @param p A person object
    */
    public void add(Person p)
    {   
        staff.put(p.getEmail(), p);
        //System.out.println("Person " + p + "added");
    }

    /**
    * Remove a Person from the organizer.
    *
    * @param email The email of the person to be removed.
    */
    public void remove(String email)
    {
        staff.remove(email);
    }

    /**
    * Remove all contacts from the organizer.
    *
    */
    public void empty()
    {
        staff.clear();
    }

    /**
    * Find the person stored in the organizer with the email address.
    * Note, each person will have a unique email address.
    * 
    * @param email The person email address you are looking for.
    *
    */
    public Person findByEmail(String email)
    {
        Person aPerson = staff.get(email);
        return aPerson;
    }

    /**
    * Find all persons stored in the organizer with the same last name.
    * Note, there can be multiple persons with the same last name.
    * 
    * @param lastName The last name of the persons your are looking for.
    *
    */
    public Person[] find(String lastName)
    {
        ArrayList<Person> names = new ArrayList<Person>();

        for (Person s : staff.values())
        {
            if (s.getLastName() == lastName) {
                names.add(s);
            }
        }
        // Convert ArrayList back to Array
        Person nameArray[] = new Person[names.size()];
        names.toArray(nameArray);
        return nameArray;
    }

    /**
    * Return all the contact from the orgnizer in
    * an array sorted by last name.
    * 
    * @return An array of Person objects.
    *
    */
    public Person[] getSortedListByLastName()
    {
        PersonLastNameComparator comp = new PersonLastNameComparator();
        Map<String, Person> sorted = new TreeMap<String, Person>(comp);


        ArrayList<Person> sortedArrayList = new ArrayList<Person>();
        for (Person s: sorted.values()) {
            sortedArrayList.add(s);
        }
        Person sortedArray[] = new Person[sortedArrayList.size()];
        sortedArrayList.toArray(sortedArray);
        return sortedArray;
    }


    private Map<String, Person> staff = new HashMap<String, Person>();

    public static void main(String[] args)
    {
        OrganizeThis testObj = new OrganizeThis();
        Person person1 = new Person("J", "W", "111-222-3333", "JW@ucsd.edu");
        Person person2 = new Person("K", "W", "345-678-9999", "KW@ucsd.edu");
        Person person3 = new Person("Phoebe", "Wang", "322-111-3333", "phoebe@ucsd.edu");
        Person person4 = new Person("Nermal", "Johnson", "322-342-5555", "nermal@ucsd.edu");
        Person person5 = new Person("Apple", "Banana", "123-456-1111", "apple@ucsd.edu");
        testObj.add(person1);
        testObj.add(person2);
        testObj.add(person3);
        testObj.add(person4);
        testObj.add(person5);

        System.out.println(testObj.findByEmail("JW@ucsd.edu"));
        System.out.println("------------" + '\n');

        Person a[] = testObj.find("W");

        for (Person p : a)
        System.out.println(p);

        System.out.println("------------" + '\n');
        a = testObj.find("W");

        for (Person p : a)
        System.out.println(p);

        System.out.println("SORTED" + '\n');
        a = testObj.getSortedListByLastName();
        for (Person b : a) {
            System.out.println(b);
        }

        System.out.println(testObj.getAuthor());
    }
}

class PersonLastNameComparator implements Comparator<Person>
{
    public int compare(Person a, Person b)
    {
        return a.getLastName().compareTo(b.getLastName());
    }
}

然后当我尝试通过创建一个匿名内部类来做到这一点时,我还得到一个构造函数 TreeMap 找不到符号错误。有什么想法吗?

内部类方法:

public Person[] getSortedListByLastName()
    {
        //PersonLastNameComparator comp = new PersonLastNameComparator();
        Map<String, Person> sorted = new TreeMap<String, Person>(new Comparator<Person>()
        {
                public int compare(Person a, Person b)
    {
        return a.getLastName().compareTo(b.getLastName());
    }
    });


        ArrayList<Person> sortedArrayList = new ArrayList<Person>();
        for (Person s: sorted.values()) {
            sortedArrayList.add(s);
        }
        Person sortedArray[] = new Person[sortedArrayList.size()];
        sortedArrayList.toArray(sortedArray);
        return sortedArray;
    }
4

2 回答 2

4

比较器应该比较,而不是地图的

也就是说,您Map包含从Stringto的映射Person,因此构造函数将需要一个 type 的参数Comparator<? super String>

要按姓氏对它们进行排序,只需使用姓氏作为键,字符串的自然排序将处理其余部分(不需要显式比较器)。

于 2010-05-15T06:44:30.257 回答
3

正如@aioobe 所说。

为了解释(例如)中的编译错误...

Map<String, Person> sorted = 
    new TreeMap<String, Person>(new Comparator<Person>() {
        public int compare(Person a, Person b) {
            return a.getLastName().compareTo(b.getLastName());
        }
    });

编译错误(实际上)抱怨它找不到类型的构造函数TreeMap<String, Person>(Comparator<Person>)。您打算使用的构造函数的真实类型签名是:

    TreeMap<K, V>(Comparator<K>)

但是您所写的内容需要一个不存在的带有签名的构造函数:

    TreeMap<K, V>(Comparator<V>)
于 2010-05-15T07:19:33.910 回答