• java===枚举,Arrays,Collections


    枚举

    package 枚举;
    /**
     * 枚举与Iterator用法相同,但是Iterator简化书写*/
    import java.util.Enumeration;
    import java.util.Vector;
    
    public class Enum {
    
        public static void main(String[] args) {
            Vector<String> v1 = new Vector<String>();
            v1.add("a");
            v1.add("bb");
            v1.add("ccc");
            v1.add("dddd");
            Enumeration<String> en1=v1.elements();
            while(en1.hasMoreElements()){
                System.out.println(en1.nextElement());
            }
    
        }
    
    }

    Arrays:

    package 容器工具类.Collections_Arrays;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    
    public class ArraysDemo {
    
        public static void main(String[] args) {
            
            //demo_1();
            //demo_2();
            demo_3();
    
        }
        public static void demo_3() {
            HashMap<Integer, String> map1 = new HashMap<Integer,String>();
            map1.put(1, "a");
            map1.put(2, "a");
            map1.put(3, "c");
            map1.put(4, "d");
            for(Integer i:map1.keySet()){
                String value=map1.get(i);
                System.out.println(i+":"+value);
            }
    //        for(map1.Entry<Integer,String> me:map1.entrySet()){
    //        Integer Key = me.getKey();
    //        String value = me.getValue();}
        }
        public static void demo_2(){
            int [] arr = {1,5,34,76,456};//new int[5];
            //二分查找  boolean不能用之外  其他都能用;
            System.out.println(Arrays.binarySearch(arr, 5));
            int [] ar=Arrays.copyOf(arr, 3);
            for (int i = 0; i < ar.length; i++) {
                System.out.print(ar[i]+",");
            }
            //数组转换成字符串
            System.out.println(Arrays.toString(arr));
            //重点:数组转成List集合  原因:集合方法更多,例如 contains indexOf .... 注意:数组长度固定所以增删方法是不可以使用的;
            //如果数组中的元素是对象,那么直接转成list元素,但是数组里面是基本类型数值,那么就将整个数组作为list的一个元素;
            List<int[]> list=Arrays.asList(arr);
            System.out.println(list);
            
        }
        public static void demo_1(){
            ArrayList<String> a1 = new ArrayList<String>();
            a1.add("abc1");
            a1.add("abc2");
            a1.add("abc3");
            a1.add("abc4");
            /*
             * for each:多用于迭代,不会用于元素操作;
             * for(类型 变量:单列集合|数组){
             *     System.out.println(变量);
             * }
             * 
             * for(String a:a1){
             *     System.out.println(a);
             * }
             * 
             * */
            //将集合转成数组  Collection的方法适用于set,list
            String[]arr=a1.toArray(new String [a1.size()]);
            for(String s1:arr){
                System.out.print(s1+",");
            }
        }
    
    }

    Collections:

    package 容器工具类.Collections_Arrays;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.TreeSet;
    
    import 比较器.comparator.ComparatorByLength;
    
    public class CollectionsDemo {
    
        public static void main(String[] args) {
            demo_2();
            
        
        }
        public static void demo(){
            List<String> l1 = new ArrayList<String>();
            l1.add("aaaaaa");
            l1.add("bbbbb");
            l1.add("cccc");
            l1.add("dd");
            //排序
            Collections.sort(l1);
            System.out.println(l1);
            //Collections.sort(l1, new ComparatorByLength());
            //System.out.println(l1);
            mySort(l1,new ComparatorByLength());
            System.out.println(l1);
            //二分查找
            int index=Collections.binarySearch(l1, "dd",new ComparatorByLength());
            System.out.println("index="+index);
            //最值
            String max=Collections.max(l1);
            System.out.println("max="+max);
            String min = Collections.min(l1, new ComparatorByLength());
            System.out.println("min="+min);
            //替换
            Collections.replaceAll(l1, "dd", "nba");
            //原理: l1.set(l1.indexOf("dd"),"nba");
        }
        public static void demo_1(){
            //逆序
            /*
            TreeSet<String> t1 = new TreeSet<String>(new Comparator<String>(){
                public int compare(String s1,String s2){
                    int temp = s2.compareTo(s1);
                    return temp;
                }
            });
            */
            //TreeSet<String> t1 = new TreeSet<String>(Collections.reverseOrder());
            TreeSet<String> t1 = new TreeSet<String>(Collections.reverseOrder(new ComparatorByLength()));
            t1.add("ab");
            t1.add("acc");
            t1.add("bbcdd");
            t1.add("cbcv");
            System.out.println(t1);
            
        }
        public static void demo_2(){
            List<String> l1 = new ArrayList<String>();
            l1.add("aaaaaa");
            l1.add("bbbbb");
            l1.add("cccc");
            l1.add("dd");
            //全部替换、初始化某种元素
            System.out.println(l1);
            //Collections.fill(l1, "*");
            System.out.println(l1);
            //随机排序
            Collections.shuffle(l1);
            System.out.println(l1);
            
        }
        public static void demo_3(){
            //重点:将不同步的集合变成同步的
            /*
            Collections.synchronizedCollection(c);
            Collections.synchronizedList(list);
            Collections.synchronizedMap(m);
            Collections.synchronizedSet(s);
            原理:
            class MyCollections{
                 public static List synList(List list){
                     return new MyList(list);
                 }
                 private class MyList{
                private static final Object lock = new Object(); 
                    private List list;
                     MyList(List list){
                         this.list=list;
                     }
                     public boolean add(Object obj){
                         synchronized(lock){
                             list.add();
                         }
                     }
                      public boolean remove(Object obj){
                         synchronized(lock){
                             list.remove();
                         }
                     }
                 }
            }
            
            */
            
        }
    /**自己实现集合排序方法*/
        private static<T> void mySort(List<T> l1,Comparator<? super T> comp) {
            for(int x=0;x<l1.size()-1;x++){
                for(int y=x+1;y<l1.size();y++){
                    if(comp.compare(l1.get(x), l1.get(y))>0){
                        //Collections.swap(l1, x, y);
                        T temp =  l1.get(x);
                        l1.set(x, l1.get(y));
                        l1.set(y, temp);//temp不能换成l1.get(x)原因是,之前已经把x位置的元素修改成了l1.get(y);
                    }
                }
            }
            
        }
    
    }
  • 相关阅读:
    为什么比特币和以太坊未必真得比EOS更去中心化?
    为什么我说IPFS社区从卖矿机开始,就是错的
    人群捕捞,不知不觉控制你
    Maven解决repository不更换的问题
    Ubuntu安装mysql-proxy
    Ubuntu的NTP同步时钟设置方法
    MySQL索引原理及慢查询优化
    MySQL对比清除不必要的表
    MySQL5.7绿色版安装
    Ubuntu下监控服务
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/6744541.html
Copyright © 2020-2023  润新知