import java.util.*;
public class TreesetTest {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
//先记录输入的总个数
int count=s.nextInt();
//记录输入的每个数
TreeSet t
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
s=new TreeSet();
for(int i=0;i<count;i++)
{
ts.add(s.nextInt());
}
//使用迭代器取出排序后的数组
Iterator it=ts.iterator();
while(it.hasNext())
{
System.out.print(it.next()+" ");
}
}
}
输出结果:
2、存储自定义类的对象实现排序的时候需要实现Comparable接口:
自定义类要实现Comparable接口
自定义类要重写comp