对面板上的点进行排序

 

package test;

import java.util.ArrayList;

import java.util.Scanner;

 

public class Test1 {

     public static void main(String[] args) {

         Point Po[]= {new Point(3,4),new Point(2,3),new Point(6,7),new Point(3,9),

                 new Point(4,5),new Point(7,8),new Point(4,8),new Point(9,4),new Point(4,4),new Point(6,4),};

         java.util.Arrays.sort(Po);

         for(Point s:Po)

            {

                System.out.println(s);

            }

         System.out.println("安装y坐标来排序");

         

         Pointy Po1[]= {new Pointy(3,4),new Pointy(2,3),new Pointy(6,7),new Pointy(3,9),

                 new Pointy(4,5),new Pointy(7,8),new Pointy(4,8),new Pointy(9,4),new Pointy(4,4),new Pointy(6,4),};

         java.util.Arrays.sort(Po1);

         for(Pointy s:Po1)

            {

                System.out.println(s);

            }

     }

 

}

 

 

 

package test;

 

public class Point implements Comparable<Point>{

    private double x;

    private double y;

   

    Point(double x,double y)

    {

        this.x=x;

        this.y=y;

    }

   

    @Override

    public int compareTo(Point o) {

        if (x > o.x) {

            return 1;

        } else if (x < o.x) {

            return -1;

        }

        else

        {

        if(y>o.y)return 1;

        else if(y<o.y)return -1;

        else return 0;

        }

    }

    public String toString()

    {

        return "("+x+","+y+")";

    }

 

}

 

 

 

 

package test;

 

public class Pointy implements Comparable<Pointy>{

    private double x;

    private double y;

   

    Pointy(double x,double y)

    {

        this.x=x;

        this.y=y;

    }

   

    @Override

    public int compareTo(Pointy o) {

        if (y > o.y) {

            return 1;

        } else if (y < o.y) {

            return -1;

        }

        else

        {

        if(x>o.x)return 1;

        else if(x<o.x)return -1;

        else return 0;

        }

    }

    public String toString()

    {

        return "("+x+","+y+")";

    }

 

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值