From f56debbad3f0a7b101843f2ecceda586ea4b093a Mon Sep 17 00:00:00 2001 From: LatteCosmos Date: Thu, 15 Jan 2026 11:37:47 +1100 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E4=BA=86=E9=98=B2=E6=AD=A2=E6=BA=A2?= =?UTF-8?q?=E5=87=BA=E7=94=A8Integer=20+=20Integer=E7=9A=84compare?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=A7=A3=E5=86=B3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 +++++ .../java/com/github/hcsp/polymorphism/Point.java | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 86a2927..3dae4a9 100644 --- a/pom.xml +++ b/pom.xml @@ -64,6 +64,11 @@ 5.6.0 test + + com.google.guava + guava + 32.1.3-jre + diff --git a/src/main/java/com/github/hcsp/polymorphism/Point.java b/src/main/java/com/github/hcsp/polymorphism/Point.java index 780e6b1..f402035 100644 --- a/src/main/java/com/github/hcsp/polymorphism/Point.java +++ b/src/main/java/com/github/hcsp/polymorphism/Point.java @@ -1,5 +1,4 @@ package com.github.hcsp.polymorphism; - import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -8,6 +7,7 @@ public class Point { private final int x; private final int y; + // 代表笛卡尔坐标系中的一个点 public Point(int x, int y) { this.x = x; @@ -22,6 +22,7 @@ public int getY() { return y; } + @Override public boolean equals(Object o) { if (this == o) { @@ -53,7 +54,16 @@ public String toString() { // 按照先x再y,从小到大的顺序排序 // 例如排序后的结果应该是 (-1, 1) (1, -1) (2, -1) (2, 0) (2, 1) - public static List sort(List points) {} + public static List sort(List points) { + points.sort((p1, p2) -> { + if (p1.getX() != p2.getX()) { + return Integer.compare(p1.getX(), p2.getX()); + } else { + return Integer.compare(p1.getY(), p2.getY()); + } + }); + return points; + } public static void main(String[] args) throws IOException { List points = @@ -66,3 +76,4 @@ public static void main(String[] args) throws IOException { System.out.println(Point.sort(points)); } } +