use*_*127 1 java arrays sorting
好的,使用你的工具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();
}}
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为您尝试对cdinventoryItem对象数组进行排序,但未cdinventoryItem实现Comparable.因此,Arrays.sort没有关于如何对数组进行排序的线索.您需要实现Comparable以确定对象的自然顺序.
例如,如果您想通过以下方式订购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;
}
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用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());
}
}
);
Run Code Online (Sandbox Code Playgroud)
PS:请尝试遵循Oracle的命名约定.例如,Java类名称应该是名词,大小写混合,每个内部单词的首字母大写.所以,cdInventoryItem你应该有,而不是CdInventoryItem.