Arrays对象

package practiceofcsp;

import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class PracticeArrays2 {
    public static void main(String[] args) {
    	Node node[]=new Node[3];
    	for(int i=0;i<node.length;i++) {
    		node[i]=new Node();//构造函数初始化
    		node[i].x=i;
    		node[i].y=i+1;
    	}
    	Arrays.sort(node);
    	System.out.println(Arrays.toString(node));
//    	Random ran = new Random();
//    	for(int i=0;i<5;i++) {
//    		System.out.println(ran.nextInt(15));
//    	}
    	
    }
}
class Node implements Comparable<Node>{
	int x;
	int y;
	@Override
	public int compareTo(Node o) {
		// TODO Auto-generated method stub
		if(x==o.x) {
			return 0;
		}
		else if(x<o.x){
			return -1;
		}
		else {
			return 1;
		}
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "["+x+","+y+"]";
	}

	
}

猜你喜欢

转载自blog.csdn.net/weixin_43893890/article/details/84700366