usemypoint.java

来自「交换数据」· Java 代码 · 共 63 行

JAVA
63
字号
/*
 * Created on 2005-10-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package chap06;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
class MyPoint implements Comparable{
	int x;
	int y;
	public MyPoint(int x,int y){
		this.x = x;
		this.y = y;
	}
	public int compareTo(Object o){
		int n = ((MyPoint)o).x;
		if(x > n)
			return 1;
		else if (x<n)
			return -1;
		else
			return 0;
	}
}
 class SortPoint {
    public MyPoint[] data = null;

    public SortPoint(MyPoint[] value) {
        data = value;
    }
    public void sort() {
        java.util.Arrays.sort(data);
    }
}
public class UseMyPoint {

	public static void main(String[] args) {
		MyPoint[] arr = new MyPoint[10];
		for(int i=0;i<arr.length;i++){
			arr[i] = new MyPoint((int)(Math.random()*10),(int)(Math.random()*10));
			System.out.println("("+arr[i].x +", "+ arr[i].y+")");
		}
			
		SortPoint sortPoint = new SortPoint(arr);
		sortPoint.sort();
		
		System.out.println("After order:");
		
		for(int i=0;i<arr.length;i++){
			System.out.println("("+arr[i].x +", "+ arr[i].y+")");
		}
		
		
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?