Java 集合排序 BigDecimal

标签 java android collections arraylist bigdecimal

我正在尝试按 BigDecimal 对 ArrayList 进行排序,但遇到了障碍。我有一个看起来有点像这样的类:

public class deal implements Comparable<deal>{

protected BigDecimal bdPrice;
protected int iQuantity;
protected String sDealType;
protected UUID dealUniqueID;
protected int dealID;
protected BigDecimal bdUnitPrice;

public deal(){
    bdPrice         = new BigDecimal("0");
    bdUnitPrice     = new BigDecimal("0");
    iQuantity       = 1;
    sDealType       = "Single item";
    dealUniqueID    = UUID.randomUUID();
    dealID          = 0;
}

private void setUnitPrice(){
    this.bdUnitPrice = this.bdPrice.divide(new BigDecimal(this.iQuantity));
}

public BigDecimal compareTo(deal oDeal) {
    // TODO Auto-generated method stub
    return bdUnitPrice.compareTo(oDeal.getUnitPrice());
}

public boolean equals(deal oDeal) {
    if (!(oDeal instanceof deal))
        return false;
    deal oD = (deal) oDeal;

    return this.bdUnitPrice.equals(oD.bdUnitPrice);
}
}

我的主要 Android Activity 如下所示:

public class SupermarketDealsActivity extends Activity {
private ArrayAdapter<deal> itemAdapter;
private ListView lvDeals;
private ArrayList<deal> itemArray;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SetUpView();
}

private void SetUpView(){
lvDeals                     = (ListView)this.findViewById(R.id.listDeals);

    itemArray                   = new ArrayList<deal>();
    itemArray.clear();

    itemAdapter                 = new ArrayAdapter<deal>(this, android.R.layout.simple_list_item_1,itemArray);
    lvDeals.setAdapter(itemAdapter);
}

private void CreateADeal(int iQuantity, BigDecimal bdPrice) {
    deal oDeal                  = new deal();

    oDeal.setQuantity(iQuantity);
    oDeal.setPrice(bdPrice);

    CreateListDeals(oDeal);
}

private void CreateListDeals(deal oDeal){
    itemArray.add(oDeal);
    Collections.sort(itemArray,Collections.reverseOrder());
    itemAdapter.notifyDataSetChanged();
}
}

在我的 java 类中,我的 compareTo 方法出现错误:

Type mismatch: cannot convert from int to BigDecimal

我一定是错过了什么,那是什么?

干杯

最佳答案

public BigDecimal compareTo(deal oDeal) {
    // TODO Auto-generated method stub
    return bdUnitPrice.compareTo(oDeal.getUnitPrice());
}

既不匹配接口(interface)的签名,也不匹配 bdUnitPrice.compareTo 的返回类型。将其更改为:

public int compareTo(deal oDeal) {
    // TODO Auto-generated method stub
    return bdUnitPrice.compareTo(oDeal.getUnitPrice());
}

我应该提到的其他一些事情(来自评论):

  1. 类名应以大写字母开头。有一个“code conventions”文档,其中详细说明了您应该做的事情和其他事情。
  2. 你重写 equals - 你不应该重写 equals 除非你也重写 hashCode - 否则你会遇到很多问题,如果你把将此类的对象转换为 HashSet、HashMap 或其他使用散列的集合。

关于Java 集合排序 BigDecimal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11254521/

相关文章:

java - 按字符串长度降序对字符串列表进行排序,不会更改列表

python - 渲染计数器集合排序顺序

java - 我的输出中收到两条错误消息

java - JVM 是 32 位还是 64 位?

java - Ant 构建不生成 jar 文件

java - 即使 PlayerView 已暂停,Exoplayer 也会播放音频

android - 确定定位服务是否开启

javascript - React Native Array.find 不是一个函数

java - 如何调试 web 应用程序中的 jar

collections - 带有子目录和自己的 index.html 的 Jekyll 集合