gpt4 book ai didi

java - Collections.sort() 不适用于实现 Comparable 的类或使用 Comparator Java 时

转载 作者:行者123 更新时间:2023-12-01 19:45:56 26 4
gpt4 key购买 nike

据我所知,我已经在类中正确实现了compareTo 方法,但是当调用Collections.sort() 时,列表似乎没有排序。我尝试使用 ArrayList 进行故障排除,以确保我正确实现一切,但没有成功。我对比较器也没有成功。看在上帝的份上,请有人帮助我!!

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CreditCard implements Comparable<CreditCard>, Serializable {

//data fields
private String lastName, firstName, PAN;
private Date expDate;

//use of SimpleDateFormat to convert strings/dates
private String pattern = "MM/yy";
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

//Constructor
public CreditCard(String firstName, String lastName, String PAN, String date) {

this.lastName = lastName;
this.firstName = firstName;
this.PAN = PAN;
try {
this.expDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
}

//Getters
public String getLastName() {
return lastName;
}

public String getFirstName() {
return firstName;
}

public String getPAN() {
return PAN;
}

public Date getExpDate() {
return expDate;
}

//Overridden methods
@Override
public String toString() {
return String.format("Name on Card: %s %s\nCard #: %s\nExp Date: %s\n",
firstName,lastName,PAN,simpleDateFormat.format(expDate));
}

@Override
public int compareTo(CreditCard o) {
if (this.getPAN().compareTo(o.getPAN()) == -1) {
return -1;
} else if (this.getPAN().compareToIgnoreCase(o.getPAN()) == 0) {
return 0;
} else {
return 1;
}
}
}

比较器

import java.io.Serializable;
import java.util.Comparator;

public class DateCompare implements Comparator<CreditCard>, Serializable{
@Override
public int compare(CreditCard o1, CreditCard o2) {
if (o1.getExpDate().compareTo(o2.getExpDate()) == -1) {
return -1;
} else if ((o1.getExpDate().compareTo(o2.getExpDate()) == 0)) {
return 0;
} else {
return 1;
}
}
}

import java.io.Serializable;
import java.util.Comparator;

public class NameCompare implements Comparator<CreditCard>, Serializable {
@Override
public int compare(CreditCard o1, CreditCard o2) {
if (o1.getLastName().compareToIgnoreCase(o2.getLastName()) == 0) {
if (o1.getFirstName().compareToIgnoreCase(o2.getFirstName()) == 0) {
return 0;
} else if (o1.getFirstName().compareToIgnoreCase(o2.getFirstName()) == -1) {
return -1;
} else {
return 1;
}
} else if (o1.getLastName().compareToIgnoreCase(o2.getLastName()) == -1) {
return -1;
} else {
return 1;
}
}
}

主要

import java.util.ArrayList;

public class CreditPayments {

//instantiate an ArrayList to hold credit cards
private ArrayList<CreditCard> list = new ArrayList<>();

//add credit cards to the list of cards
public void add(CreditCard creditCard) {
list.add(creditCard);
}

//reset the list of credit cards
public void reset() {
list.clear();
}

//Sort and print by PAN
public ArrayList<CreditCard> printByPAN() {
Collections.sort(list);
for (CreditCard cc: list) {
System.out.println(cc);
}
return list;
}

//Sort and print by Name
public ArrayList<CreditCard> printByName() {
Collections.sort(list, new NameCompare());
for (CreditCard cc: list) {
System.out.println(cc);
}
return list;
}

//Sort and print by expiration date
public ArrayList<CreditCard> printByDate() {
Collections.sort(list, new DateCompare());
for (CreditCard cc: list) {
System.out.println(cc);
}
return list;}

public static void main(String[] args) {
ArrayList<CreditCard> payments = new ArrayList<>();
payments.add( new CreditCard("bob", "smith", "1234567890", "03/17") );
payments.add( new CreditCard("bill", "smith", "9999999999", "04/18") );
payments.add( new CreditCard("bob", "smyth", "1111111111", "05/19") );
Collections.sort(payments);
for (CreditCard s: payments) {
System.out.println(s);
}
}
}

最佳答案

您应该使用Collections.sort(list)而不是list.sort(null)

如果您查看 doco 的 list.sort()您会看到它使用提供的比较器进行排序。您提供的 null (我认为)具有使您的列表保持未排序的效果。

Collections.sort()另一方面,将根据其元素的自然顺序(由 Comparable 的实现指定)对列表进行排序。

您的 CreditCard.compareTo() 实现可以简化为:

@Override
public int compareTo(CreditCard o) {
return this.PAN.compareTo(o.PAN);
}

您当前同时使用 compareTo()compareToIgnoreCase()。选择一个。

关于java - Collections.sort() 不适用于实现 Comparable 的类或使用 Comparator Java 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53358032/

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