英文:
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<Square> Dimension = new ArrayList<>();
public void addSquare(int length, int width) {
Dimension.add(new Square(length, width));
}
public void sortDimension() {
Collections.sort(Dimension, (length, width) -> 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<Dimension.size();i++ ) {
System.out.println(Dimension.get(i));
}
}
}
I found the Collections.sort(List, (obj1, obj2) -> 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
-
不要在所有地方都使用单词“Dimension”,用于类名、变量命名,以下是建议:
ArrayList<Square> dims = new ArrayList<>(); // 用于类属性 Dimension value = new Dimension(); // 在主函数中
-
当提供自定义的“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());
-
但是您可以使用“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));
}
}
英文:
-
Don't use the word
Dimension
everywhere, for the class, for naming variables, here are suggestionsArrayList<Square> dims = new ArrayList<>(); // for the class attribut Dimension value = new Dimension(); // in the main
-
When providind a custom
Comparator
, you get a pair of object and need to determine the first one, you don't get it's propertiesCollections.sort(dims, (o1, o2) -> ); // Both o1 and o2 are Square instances // You'd get for something like Collections.sort(dims, (o1, o2) -> o1.getLength() == o2.getLength() ? o1.getWidth() - o2.getWidth() : o1.getLength() - o2.getLength());
-
But you can use
Comparator
interface that provides nice methodsdims.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 "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));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论