關於Java中的二維數組__Java

來源:互聯網
上載者:User

關於Java中的二維數組[執行個體]:

public class Lesson{ public static void main(String [] args){ //二維數組的聲明方式: //資料類型 [][] 數組名稱 = new 資料類型 [長度][長度] ; //資料類型 [][] 數組名稱 = {{123},{456}} ; /* int [][] arr = {{123},{456}}; //定義了兩行三列的二維數組並賦值 for(int x = 0; x<arr.length; x++){ //定位行 for(int y = 0; y<arr[x].length; y++){ //定位每行的元素個數 System.out.print(arr[x][y]); } System.out.println("/n"); } */ int [][] num = new int [3][3]; //定義了三行三列的二維數組 num[0][0] = 1; //給第一行第一個元素賦值 num[0][1] = 2; //給第一行第二個元素賦值 num[0][2] = 3; //給第一行第三個元素賦值 num[1][0] = 4; //給第二行第一個元素賦值 num[1][1] = 5; //給第二行第二個元素賦值 num[1][2] = 6; //給第二行第三個元素賦值 num[2][0] = 7; //給第三行第一個元素賦值 num[2][1] = 8; //給第三行第二個元素賦值 num[2][2] = 9; //給第三行第三個元素賦值 for(int x = 0; x<num.length; x++){ //定位行 for(int y = 0; y<num[x].length; y++){ //定位每行的元素個數 System.out.print(num[x][y]); } System.out.println("/n"); } } } //數組值arr[x][y]表示指定的是第x行第y列的值。 //在使用二維數組對象時,注意length所代表的長度, //數組名後直接加上length(如arr.length),所指的是有幾行(Row); //指定索引後加上length(如arr[0].length),指的是該行所擁有的元素,也就是列(Column)數目。

聯繫我們

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