简体   繁体   English

在将对象放入其中时对ArrayList进行排序

[英]Sorting an ArrayList as objects are being put into them

Im currently working on a class that includes a method "add" that adds my specific object to my ArrayList which is a field instantiated in my constructor. 我目前正在处理一个包含“添加”方法的类,该方法将我的特定对象添加到ArrayList中,该对象是在构造函数中实例化的字段。 The purpose is to obviously add the object to the ArrayList but at the same time it adds the object to the position to where the ArrayList is constantly sorted. 目的显然是将对象添加到ArrayList,但同时又将对象添加到ArrayList不断排序的位置。 The objects are being compared by a String that is received from an accessor method within its class called getCompany(). 对象正在通过一个字符串进行比较,该字符串是从其名为getCompany()的类中的访问器方法接收的。 I'm having trouble understanding how I would go about doing this. 我在理解如何执行此操作时遇到了麻烦。 My experimental code follows: 我的实验代码如下:

public class WeeklyOrders {
    private List<DistributionOrder> orders;
    public WeeklyOrders(){
        orders= new ArrayList<DistributionOrder>();
    }
    public void add(DistributionOrder dOrder){
        int maxPos=0;
        int minDiff=0;
        for(int i=0;i<orders.size();i++){
            String comp=orders.get(i).getCompany();
            int diff=comp.compareTo(dOrder.getCompany());
            if(diff<minDiff){
                minDiff=diff;
                maxPos=i;
            }
        }
        orders.add(maxPos,dOrder);
    }
    public String toString(){
        String s="";
        for(int i=0;i<orders.size();i++){
            s=s+orders.get(i).getCompany()+"\t";
        }
        return s;
    }
}

You are using compareTo method incorrectly. 您使用的compareTo方法不正确。 You should not compare it return values with each other: the only sane usage of compareTo result is to compare it with zero. 您不应该相互比较它的返回值: compareTo结果的唯一合理用法是将它与零进行比较。 It returns any negative number if this element is less than passed element or any positive number if this element is bigger than passed one. 它返回任何负数如果该元素是小于传递元件,或者如果此元件比传递的一个更大的任意正数。 Probably you wanted something like this: 可能您想要这样的东西:

int maxPos=-1;
for(int i=0;i<orders.size();i++){
    String comp=orders.get(i).getCompany();
    int diff=comp.compareTo(dOrder.getCompany());
    if(diff<0){
        maxPos=i;
    } else break; // no need to continue iterating list after that
}
orders.add(maxPos+1,dOrder);

Note that you should carefully think about corner cases: what will occur if dOrder should become very first and very last element of the orders . 请注意,您应该仔细考虑极端情况:如果会发生什么dOrder应该成为的第一个和最后一个元素orders

Finally note that given the fact that the list is always sorted, you may consider using Collections.binarySearch() method with custom comparator: 最后请注意,鉴于列表始终是排序的,您可以考虑将Collections.binarySearch()方法与自定义比较器一起使用:

int maxPos = Collections.binarySearch(orders, dOrder,
                 Comparator.comparing(DistributionOrder::getCompany));
if(maxPos < 0) maxPos = - maxPos - 1;
orderds.add(maxPos, dOrder);

This would be faster. 这样会更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM