一起Talk Android吧(第九十五回:Java中的类集之工具类二)

各位看官们,大家好,上一回中咱们说的是Java中类集之工具类的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!

看官们,我们在上一章回中介绍了工具类及其提供的方法,不过都是偏向理论性的内容,而没有具体的实践,这显然不符合我们的风格。这一回中,我们将通过代码结合文本的方式来演示如何使用工具类中的功能,这样可以帮助大家进一步加深理解工具类。好了,直接上代码吧,如果看不懂代码了,直接看代码中的注释就能明白。

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class collentionsEx {
    private static List<Integer> list = new ArrayList<>();
    private static List<Integer> link = new LinkedList<>();
    private static Set<Integer> hashSet = new HashSet<>();
    private static Set<Integer> treeSet = new TreeSet<>();

    public static void main(String args[]) {

        //init the collections
        Collections.addAll(list,1,3,5,7,9);
        Collections.addAll(link,1,3,5,7,9);
        Collections.addAll(hashSet,1,3,5,7,9);
        Collections.addAll(treeSet,1,3,5,7,9);

        showCollection();

        //append some content in list, other collection can be used too.
        Collections.addAll(list, 3,4,5);
        System.out.println(" \nadd some new content in list.");
        showList(); 

        //find the content in collection,collection must be sorted, set can't be used
        int index = Collections.binarySearch(list,new Integer(3));
        if(index>=0){
            System.out.println("find 3 at: "+index);
        }else{
            System.out.println("can't find 3."+index);
        }

        //replace the content in collection, it is only used for list. 
        System.out.println(" \nreplace content 3 with 6 in list.");
        Collections.replaceAll(list, 3,6);
        showList();

        // swap the content of index a and b, it is only used for list. 
        Collections.swap(list,1, 2);
        System.out.println(" \nswap the content,which located at 1 and 2 in list.");
        showList();

        // sort the list, it is only used for list.
        Collections.sort(list);
        System.out.println(" \nsort the content in list.");
        showList();

        // revert the list, it is only used for list.
        System.out.println(" \nreverse content in list. set and map can't be reversed");
        Collections.reverse(list);
        Collections.reverse(link);
        showCollection();
    //  Collections.reverse(hashSet);
    //  Collections.reverse(treeSet);
    }

    public static void showCollection() {
        //show content of collection by Iterator
        Iterator<Integer> iter = list.iterator();
        System.out.print("List: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");

        iter = link.iterator();
        System.out.print("Link: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");

        iter = hashSet.iterator();
        System.out.print("HashSet: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");

        iter = treeSet.iterator();
        System.out.print("TreeSet: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");    
    }

    public static void showList() {
        //show content of list by Iterator
        Iterator<Integer> iter = list.iterator();
        System.out.print("List: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");
    }
}

下面是程序的运行结果,请参考:

List: 1 3 5 7 9  
Link: 1 3 5 7 9  
HashSet: 1 3 5 7 9  
TreeSet: 1 3 5 7 9  

add some new content in list.
List: 1 3 5 7 9 3 4 5  
find 3 at: 1

replace content 3 with 6 in list.
List: 1 6 5 7 9 6 4 5  

swap the content,which located at 1 and 2 in list.
List: 1 5 6 7 9 6 4 5  

sort the content in list.
List: 1 4 5 5 6 6 7 9  

reverse content in list. set and map can't be reversed
List: 9 7 6 6 5 5 4 1  
Link: 9 7 5 3 1  
HashSet: 1 3 5 7 9  
TreeSet: 1 3 5 7 9  

各位看官,关于Java中类集之工具类的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计,皆可应用在项目、毕业设计、课程设计、期末/期/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值