TreeSet是可以对字符串进行排序 的, 因为字符串已经实现了Comparable接口。
来源:互联网 发布:开微店用什么软件好 编辑:程序博客网 时间:2024/05/21 15:03
package cn.itcastset;import java.util.TreeSet;/*TreeSet是可以对字符串进行排序 的, 因为字符串已经实现了Comparable接口。字符串的比较规则: 情况一: 对应位置有不同的字符出现, 就比较的就是对应位置不同的字 符。 情况 二:对应位置上 的字符都一样,比较的就是字符串的长度。 */public class Demo7 { public static void main(String[] args) { /*TreeSet tree = new TreeSet(); tree.add("abcccccccccccccccccc"); tree.add("abc"); System.out.println(tree);*/ System.out.println("abw".compareTo("abcccccccccccc")); }}
package cn.itcastset;import java.util.Iterator;import java.util.TreeSet;/*需求:将字符串中的数值进行排序。 例如String str="8 10 15 5 2 7"; ----> "2 5 7 8 10 15"*/public class Demo8 { public static void main(String[] args) { String str="8 10 15 5 2 7"; String[] datas = str.split(" "); TreeSet tree = new TreeSet(); for(int i = 0 ; i<datas.length ; i++){ tree.add(Integer.parseInt( datas[i])); // 字符串 转int类型数据是需要使用Integer.parseInt() } //遍历treeSet的元素拼接成对应的字符串 Iterator it = tree.iterator(); while(it.hasNext()){ System.out.print(it.next()+" "); } }}
0 0
- TreeSet是可以对字符串进行排序 的, 因为字符串已经实现了Comparable接口。
- TreeSet集合存放自定义类型的对象,可以使用比较器,实现Comparable接口自己设置排序方式
- 用TreeSet对指定字符串数组内容进行排序(1)
- 实现Comparable接口对树形结构数据进行排序
- 对数组进行冒泡排序,使用comparable接口实现
- TreeSet的comparable接口
- 黑马程序员——TreeSet实现Comparable接口对自定义类排序
- 实现一个自定义字符串的排序 ,重写Comparable接口的练习!!
- 黑马程序员——TreeSet实现按字符串长度排序,复写Comparator()接口的compare()方法
- TreeSet按照字符串长度进行排序
- TreeSet对元素进行排序实现的两种方式
- 对字符串进行排序
- java 对ArrayList排序,实现Comparable接口
- java 对ArrayList排序,实现Comparable接口
- TreeSet的排序两种实现方式Comparator和Comparable
- 实现comparable接口,从写compareTo方法对object数组元素进行排序。
- 字符串、list 排序 comparable
- 写一个函数对字符串数组进行排序,排序的规则是根据每个字符串中……
- Socket通信---网络通信学习笔记(一)
- Java I/O输入输出流
- UITabBar 的使用
- HDU 5242 Game(2015年上海大都会G题)
- leetcode 019_Valid Parentheses
- TreeSet是可以对字符串进行排序 的, 因为字符串已经实现了Comparable接口。
- [leetcode] 93.Restore IP Addresses
- 惠普学习与生活第一周 /(ㄒoㄒ)/~~
- Linux系统启动流程
- 编译错误解决NO architectures to compile for。。。
- JVM内存管理机制
- POJ 1743 Musical Theme (后缀数组)
- UVa 1262 - Password(组合数)
- [leetcode]Ugly Number C语言