java - 按自然顺序对数组进行排序

标签 java arrays sorting

好的,使用您的工具比较器。它返回提示字符串值与返回 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

关于java - 按自然顺序对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4641823/

相关文章:

java - Maven 是如何知道将源文件放在 exploded 目录下的什么位置?

ios - 从 Swift 中的数组中删除当前对象

c# - 如何在 Array.Resize 期间防止 OutOfMemory 异常?

java - 从 @ResponseBody 返回 JSONArray 时抛出错误

java - 插入排序的空间复杂度不应该是O(N)吗?

javascript - 过滤和显示嵌套对象数据

java - 按特定列对多种类型多维数组进行排序

java - 线程 "main"java.util.UnknownFormatConversionException : Conversion = '-' 中出现异常

java - hibernate 如何处理冲突?

java RMI + 源命中服务器来自互联网或来自内网