java筆記11之二維數組

來源:互聯網
上載者:User

標籤:void   變化   數組   地址   數組名   群組類型   img   string   dem   

格式1:

  二維數組:就是元素為一維數組的一個數組

資料類型[][] 數組名 = new 數群組類型[m][n] 其中m為行 n為列

注意:

    A:以下格式也可以表示二維數組
              a:資料類型 數組名[][] = new 資料類型[m][n];
              b:資料類型[] 數組名[] = new 資料類型[m][n];
           B:注意下面定義的區別
              int x;
              int y;
              int x,y;
              
              int[] x;
              int[] y[];
              int[] x,y[];

 1 class Array2Demo { 2     public static void main(String[] args) { 3          //定義一個二維數組 4          int[][] arr = new int[3][2]; 5          //定義了一個二維數組arr 6          //這個二維數組有3個一維數組的元素 7          //每一個一維數組有2個元素 8          //輸出二維數組名稱 9          System.out.println(arr); //地址值    [[[email protected]10          //輸出二維數組的第一個元素一維數組的名稱11          System.out.println(arr[0]); //地址值    [[email protected]12          System.out.println(arr[1]); //地址值    [[email protected]13          System.out.println(arr[2]); //地址值    [[email protected]14          //輸出二維數組的元素15          System.out.println(arr[0][0]); //016          System.out.println(arr[0][1]); //017     }18 }

格式2

資料類型[][] 數組名 = new 資料類型[m][];
        
        m:表示這個二維數組有多少個一維數組。
        列數沒有給出,可以動態給。這一次是一個變化的列數。

 1 class Array2Demo2 { 2     public static void main(String[] args) { 3         //定義數組 4         int[][] arr = new int[3][]; 5          6         System.out.println(arr);    //[[[email protected] 7         System.out.println(arr[0]); //null 8         System.out.println(arr[1]); //null 9         System.out.println(arr[2]); //null10         11         //動態為每一個一維數組分配空間12         arr[0] = new int[2];13         arr[1] = new int[3];14         arr[2] = new int[1];15         16         System.out.println(arr[0]); //[[email protected]17         System.out.println(arr[1]); //[[email protected]18         System.out.println(arr[2]); //[[email protected]19         20         System.out.println(arr[0][0]); //021         System.out.println(arr[0][1]); //022         //ArrayIndexOutOfBoundsException23         //System.out.println(arr[0][2]); //錯誤24         25         arr[1][0] = 100;26         arr[1][2] = 200;27     }28 }

格式3

 1 class Array2Demo3 { 2     public static void main(String[] args) { 3         //定義數組 4         int[][] arr = {{1,2,3},{4,5},{6}}; 5          6         System.out.println(arr); 7         System.out.println(arr[0]); 8         System.out.println(arr[1]); 9         System.out.println(arr[2]);10         11         System.out.println(arr[0][0]); //112         System.out.println(arr[1][0]); //413         System.out.println(arr[2][0]); //614         15         System.out.println(arr[0][1]); //216         System.out.println(arr[1][1]); //517         //越界18         System.out.println(arr[2][1]); //錯誤19     }20 }

 


        基本格式:
            資料類型[][] 數組名 = new 資料類型[][]{{元素1,元素2...},{元素1,元素2...},{元素1,元素2...}};
        簡化版格式:
            資料類型[][] 數組名 = {{元素1,元素2...},{元素1,元素2...},{元素1,元素2...}};
            
        舉例:
            int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
            int[][] arr = {{1,2,3},{4,5},{6}};

二維資料圖解

 

java筆記11之二維數組

聯繫我們

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