java===枚舉,Arrays,Collections

來源:互聯網
上載者:User

標籤:字串   pac   基本   自己   操作   注意   原因   迭代   sort   

枚舉

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);                }            }        }            }}

 

java===枚舉,Arrays,Collections

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.