安卓培訓(java篇)第十天—容器(下)

來源:互聯網
上載者:User

標籤:容器   java collection collections arrays 泛型   

  1. nextInt()、nextLine()、next()三者之間的區別:

  1. nextIne():以焦點來進行輸入值;nextLine():換行輸入,焦點變化;next():不換行輸入,焦點不變。

  2. 如果nextInt()放在第一個時,需要考慮以下兩種情況:

    (1)Scanner s = new Scanner(System.in);

         System.out.println("請輸入使用者名稱:");

         int a = s.nextInt(); //有焦點集合在同一行中;

         System.out.println("請輸入密碼:");

         String b = s.nextLine(); //強制換行,此時會失去焦點,所以無法輸入


  3.   (2)Scanner s = new Scanner(System.in);


           System.out.println("請輸入使用者名稱:");

           int a=s.nextInt(); //有焦點集合在同一行中;

           System.out.println("請輸入密碼:");

           String b = s.next(); //此時是在同一行的焦點上,所以能輸入;

 

2.Map介面的兩個常用方法:

  keySet():傳回型別是Set介面;把KEY值放在Set容器裡面;

  values():傳回型別是Collection介面;把VALUE值放在Collection容器裡面;


3.Arrays:針對數組進行排序或搜尋

  常用方法:

  sort(數群組類型); //從小到大排序;

  toString(數群組類型); //返回String類型;


4.Collections:針對容器進行排序或搜尋

  常用方法:

  sort(List list); //從小到大排序;

  shuffle(List list); //使用預設隨機源對指定列表進行置換。

  reverse(List list); //反轉容器中的元素;


5.泛型的增強內容:

定義: 指的是把複雜的類型變成唯一性。必須是參考型別或者自訂類型。

常見的方法有四種:

(1)可以使用< T >來表示任意一種類型,只要主方法裡面給它類型即可;

(2)泛型可以繼承:< T extends Collection >,此時在方法裡面給定類型之後就不可變了;

(3)萬用字元:< ? extends Collection >,此時在主方法裡面可以給定任意一種超級介面下的實作類別;

(4)泛型的使用方法:必須在傳回值類型前面加< T >;如:public < T > void f( T x ) {}

 常調用的方法:System.out.println(x.getClass().getName());

注意事項:要弄清對象


課後練習:

  1. 已知:有一個二維數組:int[][] a={{5,4},{3,2},{9,7}};

    進行排序,排序後結果為:2,3,4,5,7,9;

    用到的知識點:List介面、Collections類、二維數組遍曆;


代碼:

public class PaiXu {

    public static void main(String[] args) {

        int[][] a={{5,4},{3,2},{9,7}};

        ArrayList arrayList = new ArrayList();

        //遍曆二維數組,並將結果放入容器arrayList中

        for(int[] b:a){

            for(int i:b){

                arrayList.add(i);

            }

        }

        Collections.sort(arrayList);  //對arrayList中的元素進行升序排序

        //遍曆排序後的arrayList,並逐個輸出其中的元素

        for(int j:arrayList){

            System.out.print(j);

            if(j==arrayList.get(arrayList.size()-1)){

                break;

            }

            System.out.print(",");

        }

    }

}



2.

HashMap做一個替代,{1=“小明”,2=“小東”,3=“小西”}

把“小明”替代“小紅”;然後把這裡面的value,取出來之後反轉; ,西小,東小,紅小


代碼:

public class ReplaceR {

    public static void main(String[] args) {

        HashMap<Integer,String> hashMap = new HashMap<Integer,String>();

        hashMap.put(1,"小明");

        hashMap.put(2,"小東");

        hashMap.put(3,"小西");

        hashMap.put(1,"小紅"); //把小明替代為"小紅"

        Collection<String> value = hashMap.values(); //取出value值

        LinkedList<String> list = new LinkedList<String>();

        for(String str:value){

            list.add(str);

        }

        Collections.reverse(stringBuff);

        StringBuffer stringBuff = null;

        int i==0;

        for(String str1: list) {

            stringBuff = new StringBuffer(str1);

            stringBuff.reverse();

            System.out.print(stringBuff);

            if(i==list.size()-1){   //通過i來消除輸出結果中末尾多出的一個","

                break;

            }

            System.out.print(",");

            i++;

        }

    }

}








本文出自 “坤” 部落格,請務必保留此出處http://linyingkun.blog.51cto.com/2393912/1575923

安卓培訓(java篇)第十天—容器(下)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.