java comparator sort_java – 使用Comparator对数组进行排序

我有以下代码.当我尝试编译它时,它给我以下错误:

The method sort(List, Comparator super T>) in the type Collections is not

applicable for the arguments (Software[], new Comparator(){})

The type new Comparator(){} must implement the inherited abstract method

Comparator.compare(Object, Object)

import java.text.DecimalFormat; // For proper currency

import java.util.Comparator;

import java.util.Collections;

public class Software

{

// Declare variables

String SoftwareTitle; // SoftwareTitle

int SoftwareStock; // Software totals

double SoftwarePrice; // Software Price

int SoftwareNum; // Software Product ID

double CalculateInventory; // To add inventory

double SoftwareValue; // Software Total value

double value; // Complete inventory total

Software( String softtitle, int softstock, double softprice, int softitemnum )

{

// Create object constructor

SoftwareTitle = softtitle;

SoftwareStock = softstock;

SoftwarePrice = softprice;

SoftwareNum = softitemnum;

}

// Set Software Title

public void setSoftwareTitle( String softtitle )

{

SoftwareTitle = softtitle;

}

// Return Software Title

public String getSoftwareTitle()

{

return SoftwareTitle;

}

// Set software inventory

public void setSoftwareStock( int softstock)

{

SoftwareStock = softstock;

}

// Return software inventory

public int getSoftwareStock()

{

return SoftwareStock;

}

// Set software price

public void setSoftwarePrice( double softprice )

{

SoftwarePrice = softprice;

}

// Return software price

public double getSoftwarePrice()

{

return SoftwarePrice;

}

// Set item number

public void setSoftwareNum( int softitemnum )

{

SoftwareNum = softitemnum;

} //

//return software item number

public int getSoftwareNum()

{

return SoftwareNum;

} //

// calculate inventory value

public double Softwarevalue()

{

return SoftwarePrice * SoftwareStock;

}

public void setCalculateInventory (double value){

this.CalculateInventory = value;

}

public double getCalculateInventory(){

double value = 0;

for(int i = 0; i < 3; i++){

value = Softwarevalue();

}

return value;

}

}//end method value

//

import java.text.DecimalFormat; // For proper currency

import java.util.Arrays;

import java.util.Collections;

import java.util.Comparator;

public class Inventory {

public static void main( String args[] )

{

// Start array of software titles

Software[] aSoftware = new Software[4];

aSoftware[0]= new Software("Command and Conquer ", 6, 29.99, 10122);

aSoftware[1]= new Software("Alice in Wonderland", 1, 10.99,10233);

aSoftware[2]= new Software("Doom", 1, 10.99, 10344);

aSoftware[3]= new Software("Walking Dead", 6, 9.99, 10455);

//Set currency format

DecimalFormat money = new DecimalFormat("$0.00");

// Sort in order of Software Name

Collections.sort(aSoftware, new Comparator() {

public int compare(Software s1, Software s2) {

return s1.getSoftwareTitle().compareTo(s2.getSoftwareTitle());

}

});

// Display software title, number of units, cost, item number and total inventory

for (int i = 0; i < aSoftware.length; i++){

System.out.println("Software Title is "+ aSoftware[i].getSoftwareTitle() );

System.out.println("The number of units in stock is "+ aSoftware[i].getSoftwareStock() );

System.out.println("The price of the Software Title is "+ (money.format(aSoftware[i].getSoftwarePrice() )));

System.out.println( "The item number is "+ aSoftware[i].getSoftwareNum());

System.out.println( "The value of the Software Inventory is "+ (money.format(aSoftware[i].Softwarevalue() )));

System.out.println();

}

//output total inventory value

double total = 0.0;

for (int i = 0; i < 3; i++){

total += aSoftware[i].getCalculateInventory();

}

System.out.printf("Total Value of Software Inventory is: \t$%.2f\n", total);

//end output total inventory value

}

}

//

//end

如何使用比较器按字母顺序显示软件标题(数组)?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值