(77)Collections类:sort、max、binarySearch、fill、replaceAll、reverse

这里写图片描述
这里写图片描述
这里写图片描述

public static void main(String[] args) {
        ArrayList<String> al=new ArrayList<String>();
        al.add("aa");
        al.add("abc");
        al.add("abcd");//List允许元素重复,简单理解就是他的底层是数组,数组允许元素重复
                      /*在集合的学习中,一直在学根据特定环境,元素唯一性的判断:
                       * set中不允许元素重复,数据结构为哈希表和二叉树,有各自保证元素唯一性的方法
                       * 所以误认为在list中,也有保证元素唯一性的方法,这是违背list这种数组结构的集合原意的
                       * 但是,我们可以在list中改写equals实现保证元素的唯一性:调用contains,判断这个元素在集合中是否存在
                       * contains底层调用的是equals,通过对equals改写,就能根据需求判断元素的唯一性了
                       */
        al.add("abe");
        al.add("aa");
        Collections.sort(al);//String中实现了Comparable类,自带的comparable方法,按照自然顺序排序[aa, aa, abc, abcd, abe]

        Collections.sort(al,new MyComp());//
        sop(al);//[aa, aa, abc, abe, abcd]  

         String s=Collections.max(al);
        sop("自然顺序中的max:"+s);
        String s1=Collections.max(al,new MyComp());
        sop("比较器中的max:"+s1);
//二分搜索法两种排序中寻找key
        Collections.sort(al);
        sop(al);
        int index=Collections.binarySearch(al, "abcde");
        sop(index);//返回值为-5,此数应该插入到4号位置上

        Collections.sort(al,new MyComp());
        sop(al);
        index=Collections.binarySearch(al, "aba", new MyComp());//返回值为-3,此数应该插入在2号位置
        sop(index);

        //二分搜索源代码两种方式调用
        Collections.sort(al);
        sop(al);
        index=halfSearch(al,"abe");
        sop(index); 

       Collections.sort(al,new MyComp());
        sop(al);
        index=halfSearch(al, "aba", new MyComp());
        sop(index);

        //使用指定元素替换列表中的所有元素
        String s2="hhhhhh";
        sop(al);
        Collections.fill(al, s2);
        sop(al);

        //用指定元素替换部分列表中的元素
        sop(al);
        List<String> list=partFill(al,1,3,"ooo");
        //sop(list);

        //用新值替换指定所有旧值
        boolean b=Collections.replaceAll(al, "aa", "bbb");
        sop("指定值替换是否成功:"+b);
        sop(al);

        //反转列表中的元素顺序
        sop(al);
        Collections.reverse(al);
        sop(al);


    }
    //二分搜索方法(自然顺序)
    public static int halfSearch(List<String> list,String key) {
        int low=0;
        int high=list.size()-1;
        int mid=(high-low)/2;
        while(low<=high&&list.get(mid)!=key) {
            int  i= list.get(mid).compareTo(key);
            if(i>0) 
                high--;
            if(i<0)
                low++;

            mid=(low+high)/2;

        }
        if(low<=high)
            {System.out.print("有"+key+"元素"+",元素位置为:");
            return mid;}
        else
            System.out.print("没有"+key+"元素"+",若插入该元素,插入位置为:");
          return  low; 
    }
    //二分搜索(比较器)
public static int halfSearch(List<String> list,String key,Comparator<String> cmp) {
        int low=0;
        int high=list.size()-1;
        int mid=(high-low)/2;
        while(low<=high&&list.get(mid)!=key) {
            int  i= cmp.compare(list.get(mid),key);//内部只是这发生了变化
            if(i>0) 
                high--;
            if(i<0)
                low++;

            mid=(low+high)/2;

        }
        if(low<=high)
            {System.out.print("有"+key+"元素"+",元素位置为:");
            return mid;}
        else
            System.out.print("没有"+key+"元素"+",若插入该元素,插入位置为:");
          return  low; 
    }
/*
     * 练习:fill方法可以将list集合中的所有元素替换成指定元素
     * 需求:将list集合中部分元素替换成指定元素
     */
    public static List<String> partFill(List<String> list,int start,int end,String str) {
        for(int i=start;i<=end;i++) {
            list.remove(i);//先移除这个元素,其后的元素会向前移动
            list.add(i, str);//在指定位置填入元素,之前该位置上的元素和之前其后的元素都向后移动
        }
        return list;
    }


import java.util.*;
//比较器:按照字符串长度进行比较
public class MyComp implements Comparator<String>{
    public int compare(String o1,String o2 ) {
        //主关键字为长度,次关键字为自然顺序
        int num=  o1.length()-o2.length();
        if(num==0) {
            return o1.compareTo(o2);

        }

        return num;



    }   
}
posted @ 2017-07-19 12:47  测试开发分享站  阅读(99)  评论(0编辑  收藏  举报