數組的定義和數組的遍歷

1:數組的定義

    int[] a;// 聲明數組,但並無賦值數組

        int[] a1 = new int[2];// 聲明數組,同時實例化內存,每個元素,都是0 - {0,0} //下標從0開始spa

        a1[0] = 100;.net

        a1[1] = 200;內存

       

        int[] a2 = { 1, 2 };// 聲明數組同時設置值get

        int[] a3 = new int[] { 1, 2 };test

        // 如下的聲明都是錯誤的遍歷

        // int[2] a4;bug

        // int[] a5 = new int[1]{111};錯誤

 

2:數組的遍歷

    @Test new

    public void test1() {

        int[] a = new int[] { 11, 22, 33, 44 };

        for(int i=0;i<a.length;i++){

            System.err.println(a[i]);

        }

        System.err.println("----------------------");

        //經過加強的for

        for(int aa:a){//加強的for,特色的簡單。

            System.err.println(aa);

        }  

    }

相關文章
相關標籤/搜索