Java链表的排序

package struct;

import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeSet;

public class Comp implements Comparable{

	private int x;
	private int y;
	@Override
	public int compareTo(Comp o) {
		return x-o.x;
	}
	
	Comp(int x,int y)
	{
		this.x=x;
		this.y=y;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return x+" "+y;
	}
	public static void main(String []args)
	{
		Comp c1 = new Comp(3,3);
		Comp c2 = new Comp(1,1);
		Comp c3 = new Comp(5,3);
		Comp c4 = new Comp(4,3);
		
		TreeSettreeSet1 = new TreeSet();
		System.out.println(treeSet1.add(c1));
		System.out.println(treeSet1.add(c2));
		System.out.println(treeSet1.add(c3));
		System.out.println(treeSet1.add(c4));	
		for (Comp comp : treeSet1) {
			System.out.println(comp);
		}
		
		ArrayList list = new ArrayList();
		list.add(c1);
		list.add(c2);
		list.add(c3);
		list.add(c4);
		Collections.sort(list);
		for (Comp comp : list) {
			System.out.println(comp);
		}
		
	}
}


 

你可能感兴趣的:(JAVA)