如何比较一组整数并按升序进行排序?

huangapple go评论69阅读模式
英文:

How to compare a list of integers and then sort it ascending?

问题

我正在学习如何对 ArrayList 中的正方形的维度进行排序和比较。首先按长度对正方形的维度进行排序。如果两个正方形的长度相同,则应该先考虑宽度较短的正方形。

public class Square {
    private int length;
    private int width;
    
    public Square(int length, int width) {
        this.length = length;
        this.width = width;
    }
}

public class Dimension {
    private int length;
    private int width;
    ArrayList<Square> Dimension = new ArrayList<>();
    
    public void addSquare(int length, int width) {
        Dimension.add(new Square(length, width));
    }

    public void sortDimension() {
        Collections.sort(Dimension, (square1, square2) -> {
            if (square1.length == square2.length) {
                return Integer.compare(square1.width, square2.width);
            }
            return Integer.compare(square1.length, square2.length);
        });
    }
 
    public static void main(String[] args) {
        Square square;
        Dimension dimension = new Dimension();
        dimension.addSquare(10, 5);
        dimension.addSquare(8, 8);
        dimension.addSquare(10, 2);
        dimension.addSquare(12, 10);
        dimension.addSquare(8, 5);
        dimension.sortDimension();

        for(int i = 0; i < dimension.Dimension.size(); i++) {
            System.out.println(dimension.Dimension.get(i));
        }
    }
}

我在互联网上找到了 Collections.sort(List, (obj1, obj2) -> obj1.compareTo(obj2)),但它在我的代码中不起作用。编译器显示 无法从 Obj 转换为 int

关于这个问题,我需要您的建议。谢谢。

英文:

I am learning how to sort and compare the dimension of squares in ArrayList. The dimension of the squares first ordered by length. If two squares have the same length, the squares requiring the shorter width should come first.

public class Square {
private int length;
private int width;
public Square(int length, int width) {
this.length = length;
this.width = width;
}
}
public class Dimension {
private int length;
private int width;
ArrayList&lt;Square&gt; Dimension = new ArrayList&lt;&gt;();
public void addSquare(int length, int width) {
Dimension.add(new Square(length, width));
}
public void sortDimension() {
Collections.sort(Dimension, (length, width) -&gt; length.compareTo(width));
}
public static void main(String[] args) {
Square square;
Dimension dimension = new Dimension();
dimension.addSquare(10, 5);
dimension.addSquare(8, 8);
dimension.addSquare(10, 2);
dimension.addSquare(12, 10);
dimension.addSquare(8, 5);
dimension.sortDimension();
for(int i=0; i&lt;Dimension.size();i++ ) {
System.out.println(Dimension.get(i));
}
}
}

I found the Collections.sort(List, (obj1, obj2) -&gt; obj1.compareTo(obj2)) on the internet, but it does not work on my code. The compiler says cannot convert from Obj to int.

I need your suggestion on this matter. Thank you.

答案1

得分: 1

  1. 不要在所有地方都使用单词“Dimension”,用于类名、变量命名,以下是建议:

    ArrayList<Square> dims = new ArrayList<>(); // 用于类属性
    
    Dimension value = new Dimension(); // 在主函数中
    
  2. 当提供自定义的“Comparator”时,您会得到一对对象并且需要确定第一个对象,您无法获取它的属性:

    Collections.sort(dims, (o1, o2) -> ); // o1 和 o2 都是 Square 实例
    
    // 您可以这样处理
    Collections.sort(dims, (o1, o2) -> o1.getLength() == o2.getLength() ?
                                       o1.getWidth() - o2.getWidth() :
                                       o1.getLength() - o2.getLength());
    
  3. 但是您可以使用“Comparator”接口提供的便捷方法:

    dims.sort(Comparator.comparing(Square::getLength).thenComparing(Square::getWidth));
    

然后在主函数中访问列表时,您需要从实例中获取,如 value.dims.size()。并且通过在 Square 类中使用漂亮的 toString 方法,会更好。

class Square {
    private int length, width;
    public Square(int length, int width) {
        this.length = length;
        this.width = width;
    }
    public int getLength() {return length;}
    public int getWidth() {return width; }
    @Override
    public String toString() {return "Square{" + "length=" + length + ", width=" + width + '}';}
}

class Dimension {

    private ArrayList<Square> dims = new ArrayList<>();

    public static void main(String[] args) {
        Dimension value = new Dimension();
        value.addSquare(10, 5);
        value.addSquare(8, 8);
        value.addSquare(10, 2);
        value.addSquare(12, 10);
        value.addSquare(8, 5);
        value.sortDimension();

        for (int i = 0; i < value.dims.size(); i++) {
            System.out.println(value.dims.get(i));
        }
    }

    public void addSquare(int length, int width) {
        dims.add(new Square(length, width));
    }
    public void sortDimension() {
        dims.sort(Comparator.comparing(Square::getLength).thenComparing(Square::getWidth));
    }
}
英文:
  1. Don't use the word Dimension everywhere, for the class, for naming variables, here are suggestions

    ArrayList&lt;Square&gt; dims = new ArrayList&lt;&gt;(); // for the class attribut
    Dimension value = new Dimension(); // in the main
    
  2. When providind a custom Comparator, you get a pair of object and need to determine the first one, you don't get it's properties

    Collections.sort(dims, (o1, o2) -&gt; ); // Both o1 and o2 are Square instances
    // You&#39;d get for something like
    Collections.sort(dims, (o1, o2) -&gt; o1.getLength() == o2.getLength() ?
    o1.getWidth() - o2.getWidth() :
    o1.getLength() - o2.getLength());
    
  3. But you can use Comparator interface that provides nice methods

    dims.sort(Comparator.comparing(Square::getLength).thenComparing(Square::getWidth));
    

Then in the main to access the list you need to go from the instance, as value.dims.size(). And with a nice toString in Square class it' be ok

class Square {
    private int length, width;
    public Square(int length, int width) {
        this.length = length;
        this.width = width;
    }
    public int getLength() {return length;}
    public int getWidth() {return width; }
    @Override
    public String toString() {return &quot;Square{&quot; + &quot;length=&quot; + length + &quot;, width=&quot; + width + &#39;}&#39;;}
}

class Dimension {

    private ArrayList&lt;Square&gt; dims = new ArrayList&lt;&gt;();

    public static void main(String[] args) {
        Dimension value = new Dimension();
        value.addSquare(10, 5);
        value.addSquare(8, 8);
        value.addSquare(10, 2);
        value.addSquare(12, 10);
        value.addSquare(8, 5);
        value.sortDimension();

        for (int i = 0; i &lt; value.dims.size(); i++) {
            System.out.println(value.dims.get(i));
        }
    }

    public void addSquare(int length, int width) {
        dims.add(new Square(length, width));
    }
    public void sortDimension() {
        dims.sort(Comparator.comparing(Square::getLength).thenComparing(Square::getWidth));
    }
}

huangapple
  • 本文由 发表于 2020年10月25日 05:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64518072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定