java 二維數組

來源:互聯網
上載者:User

標籤:void   一個   color   string   組元   post   str   種類   +=   

二維數組

多維陣列可以簡單地理解為在數組中嵌套數組

二維數組的定義格式

二維數組的定義有很多方式

第一種方式

1 int[][] arr = new int[3][4];

上面的代碼相當於定義了一個3*4的二維數組,即二維數組的長度為3,二維數組中的每個元素又是一個長度為4的數組

第二種方式

1 int[][] arr = new int[3][];

第二種方式和第一種類似,只是數組中每個元素的長度不確定

第三種方式

1 int[][] arr = {{1,2},{3,4,5,6},{7,8,9}};

上面的二維數組中定義了三個元素,這三個元素都是數組,分別為{1,2}、{3,4,5,6}、{7,8,9}

對二維數組中元素的訪問也是通過角標的方式,例如訪問二維數組中第一個元素數組的第二個元素

1 arr[0][1];
二維數組元素的訪問

如何擷取二維數組中元素值

例:

 1 class ArrayDemo08 { 2     public static void main(String[] args){ 3      4         //定義二維數組的方式 5         int[][] arr = new int[3][4]; 6      7         System.out.println( arr ); 8         System.out.println("二維數組的長度: " + arr.length); 9         //擷取二維數組的3個元素10         System.out.println( arr[0] );11         System.out.println( arr[1] );12         System.out.println( arr[2] );13         14         System.out.println("列印第一個一維數組的元素值");15         System.out.println( arr[0][0] );16         System.out.println( arr[0][1] ); //訪問的為二維數組中第1個一維數組的第2個元素17         System.out.println( arr[0][2] );18         System.out.println( arr[0][3] );19         20         System.out.println("列印第二個一維數組的元素值");21         System.out.println( arr[1][0] );22         System.out.println( arr[1][1] );23         System.out.println( arr[1][2] );24         System.out.println( arr[1][3] );25         26         System.out.println("列印第三個一維數組的元素值");27         System.out.println( arr[2][0] );28         System.out.println( arr[2][1] );29         System.out.println( arr[2][2] );30         System.out.println( arr[2][3] );31     }
二維數組元素遍曆與數組元素累加和
 1 class ArrayDemo09 { 2     public static void main(String[] args){ 3         //一維數組的求累加和並遍曆 4         int[] arr = {10,20,30,40,50}; 5         int sum = 0;  6         for (int i=0; i<arr.length; i++) { 7               //System.out.println(arr[i]); 8             sum += arr[i]; 9         }10         System.out.println("sum= " + sum);11         System.out.println("---------------------");12         13     //二維數組的求累加和並遍曆14         int[][] arr2 = { {1,2},{3,4,5},{6,7,8,9,10} };15         int sum2 = 0;16         for (int i=0; i<arr2.length; i++) {17             for (int j=0; j<arr2[i].length; j++) {18                  //System.out.println(arr2[i][j])19                 sum2 += arr2[i][j];20             }21         }22         System.out.println("sum2= "+ sum2);23     }24 }25 結果:sum=15026      sum2=55

 

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.