排序 ArrayList 时发生 Collections.sort() 错误



我正在尝试按名称排序 ArrayList 元素,但我无法解决问题。 . . . . .

有人可以帮忙吗?

错误 在case 4: Collections.sort(contact);

错误"必需类型: 提供的列表: 列表原因: 否 存在类型为变量 T 的实例,以便数据符合 可比<?超级T>">

下面的代码无需排序即可正常工作


public class AddressBook {
private static List<Data> contact = new ArrayList<Data>();
public static void main(String[] args) {

AddressBook addressBook = new AddressBook();
Scanner sc = new Scanner(System.in);
int menu;
String choice;
String choice2;
System.out.println(" =========================== ");
System.out.println(" | 0. Exit.                |");
System.out.println(" | 1. Add contact.         |");

System.out.println(" =========================== ");

try
{
menu = sc.nextInt();
while (menu != 0) {
switch (menu) {
case 1:
while (menu != 2) {
System.out.println("Enter First Name: ");
String firstName = sc.next();
System.out.println("Enter Last Name: ");
String lastName = sc.next();
System.out.println("Enter Phone: ");
String homePhone = sc.next();
if (homePhone.length()!=11 || !homePhone.startsWith("8")) {
System.out.println("Number should start with '8' and has '11' digit" );
}else {
System.out.println("Enter Email: ");
String personalWebSite = sc.next();
contact.add(new Data(firstName, lastName,
homePhone, personalWebSite));
}
System.out
.println("Would you like to add someone else? 1: Yes, 2: No");
menu = sc.nextInt();
}
break;
case 2:
System.out
.println("Enter First Name of contact that you would like to edit: ");
choice = sc.next();
addressBook.deleteByFirstName(choice);
System.out.println("Enter First Name: ");
String firstName = sc.next().toUpperCase();
System.out.println("Enter Last Name: ");
String lastName = sc.next();
System.out.println("Enter Phone: ");
String homePhone = sc.next();
System.out.println("Enter Email: ");
String personalWebSite = sc.next();
contact.add(new Data(firstName, lastName,
homePhone, personalWebSite));
break;
case 3:
System.out.println("------------------");
System.out.println("1. Search number: ");
System.out.println("2. Search name: ");
System.out.println("------------------");
int search= sc.nextInt();
if(search==1) {
System.out
.println("Enter Number of contact: ");
choice2 = sc.next();
addressBook.searchByPhoneNumber(choice2);
break;
}else {
System.out
.println("Enter First Name of contact: ");
choice = sc.next();
addressBook.searchByFirstName(choice);
break;
}
case 4:
Collections.sort(contact);
//ERROR occurring here 


case 5:
System.out.println("This is a list of every contact");
System.out.println(addressBook.contact);
break;
case 6:
System.out.println("------------------");
System.out.println("1. Delete by name: ");
System.out.println("2. Delete all: ");
System.out.println("------------------");
int del= sc.nextInt();
if(del==1){
System.out
.println("Enter First Name of contact that you would like to delete: ");
choice = sc.next();
addressBook.deleteByFirstName(choice);
break;
}else{
System.out.println("Successfully Deleted");
System.out.println("");
contact.clear();
}
break;
default:
throw new IllegalStateException("Unexpected value: " + menu);
}
System.out.println(" =========================== ");
System.out.println(" | 0. Exit.                |");
System.out.println(" | 1. Add contact.         |");

System.out.println(" =========================== ");
menu = sc.nextInt();
}
}
catch(InputMismatchException exception)
{
System.out.println("This is not an integer");
}
System.out.println("Good-Bye!");
}
private void searchByFirstName(String firstName) {
for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
Data temp = iterator.next();
if (temp.getFirstName().equalsIgnoreCase(firstName)) {
System.out.println(temp);
return;
}
}

System.out.println("No contact with first name " + firstName
+ " was found.");
}
private void searchByPhoneNumber(String homePhone) {
for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
Data temp = iterator.next();
if (temp.getHomePhone().equalsIgnoreCase(homePhone)) {
System.out.println(temp);
return;
}
}
System.out.println("No contact with number " + homePhone
+ " was found.");
}

private void deleteByFirstName(String firstName) {
for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
Data temp = iterator.next();
if (temp.getFirstName().equalsIgnoreCase(firstName)) {
iterator.remove();
return;
}
}
System.out.println("No contact with first name " + firstName
+ " was found.");
}
private void deleteAll(String all) {
for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
iterator.remove();
return;
}
System.out.println("Deleting...");
}
private static int[] selectionSortAlg(int[] a, int n) {
for (int i = 0; i < n - 1; i++) {
int iMin = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[iMin]) {
iMin = j; // index of smallest element
}
}
int temp = a[i];
a[i] = a[iMin];
a[iMin] = temp;
System.out.println("Pass..." + i + "..." + Arrays.toString(a));
}
return a;
}

public static class Data {
private String firstName = null;
private String lastName = null;
private String homePhone = null;
private String personalWebSite = null;
public Data(String firstName,String lastName, String homePhone, String personalWebSite) {
this.firstName = firstName;
this.lastName = lastName;
this.homePhone = homePhone;
this.personalWebSite = personalWebSite;
}
public String getFirstName() {
return firstName;
}
public String getHomePhone() {
return homePhone;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}


public String toString() {
return String.format(firstName+" "+lastName+" "+homePhone+" "+personalWebSite);
}
}
}
class T {
private String firstName = null;
private String lastName = null;
private String homePhone = null;
private String personalWebSite = null;
public T(String firstName,String lastName, String homePhone, String personalWebSite) {
this.firstName = firstName;
this.lastName = lastName;
this.homePhone = homePhone;
this.personalWebSite = personalWebSite;
}
public String getFirstName() {
return firstName;
}
public String getHomePhone() {
return homePhone;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}


public String toString() {
return String.format(firstName+" "+lastName+" "+homePhone+" "+personalWebSite);
}
}

我不太确定,但我认为您需要这样做(在您的案例 4 中(:

Collections.sort(contact, new Comparator<Data>() {
@Override
public int compare(Data contact1, Data contact2) {
return contact1.getFirstName().compareTo(contact2.getFirstName());
}
});

试一试,我在其他地方用过它,它奏效了。希望对您有所帮助。干杯:)

以这种方式使用contact.sort( Comparator.comparing(Data::getFirstName) );

您有 2 个选项,无论是实现可比较还是提供比较器到排序功能,即

Collections.sort(contact, (d1,d2) -> d1.firstName.compareTo(d2.firstName));

contact.sort((d1,d2) -> d1.firstName.compareTo(d2.firstName));

你实现的class Data可比,看看这里:

https://www.geeksforgeeks.org/comparable-vs-comparator-in-java/

public static class Data implements Comparable<Data>
...
public int compareTo(Data m) {
...
}

要么在排序方法中传递比较器,要么让数据类实现可比较接口并实现 compareTo((

@GiorgosDev给出了一个示例,其中数据类有望实现类似的接口。

要么在数据类中实现可比较,要么在排序方法中传递比较器。Example Collections.sort(contact, Comparator.comparing(Data::getFirstName((;

最新更新