如何比较整数列表然后对其进行升序排序?
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, (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));
}
}
}
我在互联网上找到了 Collections.sort(List, (obj1, obj2) -> obj1.compareTo(obj2))
,但它不适用于我的代码。编译器说 cannot convert from Obj to int
.
我需要你对这件事的建议。谢谢。
不要处处使用Dimension
这个词,对于class,变量命名,这里有一些建议
ArrayList<Square> dims = new ArrayList<>(); // for the class attribut
Dimension value = new Dimension(); // in the main
当提供自定义Comparator
时,你得到一对对象,需要确定第一个,你没有得到它的属性
Collections.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());
但是你可以使用 Comparator
接口,它提供了很好的方法
dims.sort(Comparator.comparing(Square::getLength).thenComparing(Square::getWidth));
然后在主要访问您需要从实例中访问的列表,如value.dims.size()
。在 Square
class 中有一个很好的 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));
}
}
我正在学习如何对 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, (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));
}
}
}
我在互联网上找到了 Collections.sort(List, (obj1, obj2) -> obj1.compareTo(obj2))
,但它不适用于我的代码。编译器说 cannot convert from Obj to int
.
我需要你对这件事的建议。谢谢。
不要处处使用
Dimension
这个词,对于class,变量命名,这里有一些建议ArrayList<Square> dims = new ArrayList<>(); // for the class attribut Dimension value = new Dimension(); // in the main
当提供自定义
Comparator
时,你得到一对对象,需要确定第一个,你没有得到它的属性Collections.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());
但是你可以使用
Comparator
接口,它提供了很好的方法dims.sort(Comparator.comparing(Square::getLength).thenComparing(Square::getWidth));
然后在主要访问您需要从实例中访问的列表,如value.dims.size()
。在 Square
class 中有一个很好的 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));
}
}