【问题标题】:How to sort an arrayList of objects by their components in java如何在java中按对象的组件对对象的arrayList进行排序
【发布时间】:2012-04-24 03:35:41
【问题描述】:

我正在尝试以两种不同的方式对 arrayList 进行排序,一种是通过 arrayList 中对象的区域,另一种是通过 arrayList 中对象的名称(shape1,shape2)。当我将对象打印到文件中时,它们看起来像这样: shape1: (points, radius, etc...) area = 0.0 并且形状继续存在。我尝试查看其他类似但都使用 Collections.sort 回答的问题。我不确定我应该使用这种方法。这是我正在使用的一些代码,可以为您提供一个想法:

for (int i =0; i <shapes.size();i++){
    for (int j = 1; j<shapes.size(); j++){
        if (shapes.get(i).getShape().area() > shapes.get(j).getShape().area())
        {
            //
        }
        else
        {
            //
        }
    }
}

我不知道该怎么做。任何指针?对于按名称排序,我必须使用:

shapes.get(i).getName()

【问题讨论】:

    标签: java


    【解决方案1】:

    解决方案 1

    您的对象可以实现Comparable 接口并使用Collections.sort(List list) 进行排序。

    public class Shape implements Comparable<Shape> {
        @Override
        public int compareTo(Shape o) {
            if(o == null) {
                return 1;
            }
            if(getName() == null || o.getName() == null) {
                return 0;
            } else if(getName() != null && o.getName() == null) {
                return 1;
            } else if(getName() == null && o.getName() != null) {
                return -1;
            }
            return getName().compareTo(o.getName());
        }
    }
    
    Collections.sort(shapes);
    

    解决方案 2

    创建一个实现Comparator的类并使用Collections.sort(List list, Comparator c)

    public class ShapeComparator implements Comparator<Shape> {
        @Override
        public int compare(Shape s1, Shape s2) {
            if(s1 == null || s2 == null) {
                return 0;
            } else {
                return s1.getName().compareTo(s2.getName());
            }
        }
    }
    
    Collections.sort(shapes, new ShapeComparator());
    

    【讨论】:

    • 我已经取消了您的 Comparable 实施。缺少一个泛型类型参数,这就是(我假设)你所说的“强类型”废话。也就是说,两种实现都不能处理左侧getName() 调用返回null 的情况。它会是 NPE。
    【解决方案2】:

    由于这是作业,我不会发布任何代码。

    如果你不允许使用Arrays.sort,你可以实现Selection Sort——它非常简单,你的代码中已经有了它的开始。这个想法是在i 上的外循环的每次迭代中,使用j 上的内循环来选择从ishapes.size() 的段中的最小元素,并将该元素放置在i-th你的数组的位置。你的内部循环应该是这样的:

    for(int j = i+1 ; j<shapes.size(); j++)
    //          ^--- this is what's changed
    

    现在根据您的if 条件,您可以将j-th 元素与i-th 交换,或者将其保持在原位并继续。

    要对字符串进行排序,请在 if 条件中使用 compareTo 方法。

    【讨论】:

    • 如何将它放在第 i 个位置? 。添加?还是 .set?
    • @Milwaukoholic 使用set。经典的交换是这样的:Shape tmp=shapes.get(i); shapes.set(i, shapes.get(j)); shapes.set(j, tmp);
    【解决方案3】:
    【解决方案4】:

    我认为你应该使用这样的东西:

            Collections.sort(shapes, new Comparator<Object>() {
                public int compare(Object obj1, Object obj2) {
                    Shape shape1 = ((Shape) obj1).getShape();
                    Shape shape2 = ((Shape) obj2).getShape();
    
                    String name1 = ((Shape) obj1).getName();
                    String name2 = ((Shape) obj1).getName();
    
                    Double area1 = shape1.area();
                    Double area2 = shape2.area();
    
                    int areaCmp = area1 - area2;
                    if( areaCmp!= 0 ) {
                        return areaCmp;
                    }
    
                    return name1.compareTo(name2);
                }
            });
    

    了解更多info

    【讨论】:

      猜你喜欢
      • 2012-04-26
      • 2014-11-09
      • 2017-12-12
      • 1970-01-01
      • 2011-03-21
      • 2011-05-11
      • 1970-01-01
      • 2013-05-21
      • 2013-01-06
      相关资源
      最近更新 更多