java集合、常用类和String

java集合、常用类和String

ID:37490660

大小:217.00 KB

页数:24页

时间:2019-05-24

java集合、常用类和String_第1页
java集合、常用类和String_第2页
java集合、常用类和String_第3页
java集合、常用类和String_第4页
java集合、常用类和String_第5页
资源描述:

《java集合、常用类和String》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、第五部分Java中的集合集合可以放置不同类型的数据,长度也是可以改变的,集合类都在java.util包中,包括:set、List、Map、等等。Collection接口是集合类的根接口,主要方法介绍:Booleanadd(Eo)指定元素追加到列表最后BooleanaddAll(Collectionc)将c中的所有元素追加Voidclear()Booleancontains(objecte)判断是否含有指定元素BooleancontainAll(Collectionc)判断是否包含全部inthashCode()返回次集合的哈希码

2、值BooleanisEmpty()Ineratoriteratorvoidremove(intindex);删除指定位置上的元素BooleanremoveAll(Collectionc)删除与集合c中相同的元素intsize()toArray()和数组形式的转换1.集合setSet实现了Collection中的所有方法,不能有重复元素,主要的类HashSet和TreeSet(1)HashSet对元素随机排序的集合类,无序不重复集合,如果元素个数超过了集合容量,会自动增加一倍;例子说名问题:importjava.util.HashSet;pub

3、licclassHashSetTest{publicstaticvoidmain(Stringargs[]){HashSethashSet=newHashSet();hashSet.add("red");hashSet.add("yellow");hashSet.add("blue");hashSet.add("white");hashSet.add("black");hashSet.add(8);System.out.println("hashSet'sinfo:"+hashSet);hashSet.remove("red");System.out.println("

4、hashSet:"+hashSet.toString());System.out.println("thesizeofhashSetis:"+hashSet.size());Object[]Value=hashSet.toArray();for(inti=0;i

5、'ssize:"+hashSet.size());}}运行结果:hashSet'sinfo:[red,white,8,blue,yellow,black]hashSet:[white,8,blue,yellow,black]thesizeofhashSetis:5white8blueyellowblackfalsethehashSet'ssize:0(1)TreeSet类对元素排序的有序集合集合中元素是自然排序,不能有重复元素例子说明:importjava.util.TreeSet;importjava.util.SortedSet;publicclassTreeSet

6、Test{publicstaticvoidmain(Stringargs[]){SortedSettreeSet=newTreeSet();treeSet.add("10");treeSet.add("red");treeSet.add("yellow");treeSet.add("blue");treeSet.add("white");System.out.println("first_treeSet="+treeSet.first());System.out.println("last_treeSet="+treeSet.last());//System.out.p

7、rintln("subSet_treeSet="+treeSet);System.out.println("treeSet'length:"+treeSet.size());System.out.println("treeSetisContainred:"+treeSet.contains("red"));treeSet.clear();System.out.println(treeSet.isEmpty());}}运行结果:first_treeSet=10last_treeSet=yellowsubSet_treeSet=[10,blu

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。