用Java创建TreeSet
创建一个TreeSet并添加元素-
TreeSet<String> tSet = new TreeSet<String>(); tSet.add("TV"); tSet.add("Radio"); tSet.add("Internet");
遍历元素-
Iterator<String> i = tSet.iterator(); while(i.hasNext()){ System.out.println(i.next()); }
以下是创建TreeSet的示例-
示例
import java.util.*; public class Demo { public static void main(String args[]){ TreeSet<String> tSet = new TreeSet<String>(); tSet.add("TV"); tSet.add("Radio"); tSet.add("Internet"); Iterator<String> i = tSet.iterator(); while(i.hasNext()){ System.out.println(i.next()); } } }
输出结果
Internet Radio TV