OK,使用您的工具Comparator。它返回错误,提示字符串值与return int不兼容。
class cdinventoryItem implements Comparable<cdinventoryItem> {
private String Ptitle;
private int PitemNumber;
private int PnumberofUnits;
private double PunitPrice;
private double Evalue;
public cdinventoryItem(String title, int itemNumber, int numberofUnits, double unitPrice ){
Ptitle = title;
PitemNumber = itemNumber;
PnumberofUnits = numberofUnits;
PunitPrice = unitPrice;
}
public int compareTo(cdinventoryItem otherItem) {
return this.Ptitle.compareTo(otherItem.getTitle());
}
public int getTitle() {
return Ptitle;
}
public double stockValue () {
return PnumberofUnits * PunitPrice;
}
public double getEntireStockValue () {
Evalue = Evalue + this.stockValue();
return Evalue;
}
@Override public String toString(){
NumberFormat dformat = new DecimalFormat("#0.00");
StringBuilder ouput = new StringBuilder();
String New_Line = System.getProperty("line.separator");
ouput.append ("The product number of my CD is: " + PitemNumber + New_Line);
ouput.append ("The title of the CD is: " + Ptitle + New_Line);
ouput.append ("I have " + PnumberofUnits + " units in stock." + New_Line);
ouput.append ("The total value of my inventory on this product is: " + dformat.format (stockValue()) + New_Line);
return ouput.toString();
}
}
public class cdinventoryprogram {
public static void main(String[] args) {
NumberFormat dformat = new DecimalFormat("#0.00");
double Tvalue= 0.0;
int DEFAULT_LENGTH = 3;
System.out.println ("Welcome to my CD inventory program!!");
System.out.println ("");
cdinventoryItem initem[] = new cdinventoryItem [DEFAULT_LENGTH];
initem [0] = new cdinventoryItem ("The Illusionist", 1, 5, 15.99);
initem [1] = new cdinventoryItem ("Matrix", 2, 3, 14.99);
initem [2] = new cdinventoryItem ("Old School", 3, 6, 12.99);
Arrays.sort(initem,
new Comparator<cdinventoryItem>() {
public int compare(cdinventoryItem item1, cdinventoryItem item2) {
return item1.getTitle().compareTo(item2.getTitle());
}});
for ( cdinventoryItem currentcdinventoryItem : initem ){
System.out.println( currentcdinventoryItem );
Tvalue = Tvalue + currentcdinventoryItem.getEntireStockValue();
}
System.out.println ("The total value of my entire inventory is:");
System.out.println ( "$" + dformat.format (Tvalue));
System.out.println();
}}
最佳答案
发生这种情况是因为您试图对cdinventoryItem
对象的数组进行排序,但是cdinventoryItem
没有实现Comparable
。因此,Arrays.sort
不了解如何对数组进行排序。您需要实现Comparable
以确定对象的natural order。
例如,如果要按title
进行订购:
public class cdinventoryItem implements Comparable<cdinventoryItem> {
// your code
public int compareTo(cdinventoryItem otherItem) {
return this.Ptitle.compareTo(otherItem.getTitle());
}
public String getTitle() {
return Ptitle;
}
}
另外,您可以使用
Arrays.sort(T[], java.util.Comparator)
,并定义要使用的自定义排序方法:cdinventoryItem initem[] = new cdinventoryItem[DEFAULT_LENGTH];
// fill array
Arrays.sort(initem,
new Comparator<cdInventoryItem>() {
public int compare(cdInventoryItem item1, cdInventoryItem item2) {
return item1.getTitle().compareTo(item2.getTitle());
}
}
);
PS:请尝试遵循Oracle's naming conventions。例如,Java类名应为名词,每个内部单词的第一个字母都用大写字母混合。因此,您应该有
cdInventoryItem
而不是CdInventoryItem
。