gpt4 book ai didi

java - 在 Java 中传递比较器语法帮助

转载 作者:行者123 更新时间:2023-11-29 07:22:02 25 4
gpt4 key购买 nike

我已经尝试了几种方法,第一种是在以下代码的底部有一个实现比较器的类。当我尝试在 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;
}

最佳答案

比较器应该比较 map 的,而不是

也就是你的Map包含来自 String 的映射至 Person , 因此构造函数将期望类型为 Comparator<? super String> 的参数.

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

关于java - 在 Java 中传递比较器语法帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2839154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com