现在的位置: 首页 > 综合 > 正文

用TreeSet对指定字符串数组内容进行排序(1)

2014年07月03日 ⁄ 综合 ⁄ 共 548字 ⁄ 字号 评论关闭
package test_set_map;

import java.util.Iterator;
import java.util.TreeSet;

public class Test_Tree {

	/**
	 * @param args
	 * 
	 *            String demos[]={"hello","donghongyu","test","中国"};//排序
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String demos[] = { "chun", "hello", "test", "中国", "test" };// 与set相同不能有重复元素

		TreeSet<String> str = new TreeSet<String>();

		for (int i = 0; i < demos.length; i++) {
			str.add(demos[i]);// 循环遍历并添加到TreeSet集合中
		}
		Iterator<String> itr = str.iterator();

		if (str.comparator() != null) {
			System.out.println("不是自然排序");
		}

		while (itr.hasNext()) {
			System.out.println(itr.next());
		}

	}

}

抱歉!评论已关闭.