Java中二維數組的操作

來源:互聯網
上載者:User

標籤:font   長度   net   toc   ber   initial   返回   get   image   

//1.二維數組的定義

//2.二維數組的記憶體空間

 //3.不規則數組   



 

 輸出要放在迴圈裡面,放在外面就報錯了

 打debug確定二維數組的關係

 

現在程式在7與8行進行迴圈

------------------------------------------------------------------------------------------------------------------------------------

然後經曆一個中間階段

然後繼續

 

 一直到

就結束了


 

二維數組記憶體結構 

 

 不規則二維數組 
  1. //1.二維數組的定義  
  2. //2.二維數組的記憶體空間  
  3. //3.不規則數組  
  4. package me.array;  
  5. public class Array2Demo{  
  6.       
  7.       
  8.     public static void main(String[] args){  
  9.           
  10.         //建立和列印不規則二維數組  
  11.         int arr[ ][ ];  
  12.           
  13.         arr=new int[3][];//現在說明為不規則數組  
  14.           
  15.         arr[0]=new int[10];//arr[0]指向另一個一位元組  
  16.         arr[1]=new int[3];  
  17.         arr[2]=new int[4];  
  18.           
  19.         //賦值  
  20.         for(int i=0;i<arr.length;i++){  
  21.               
  22.             for(int j=0;j<arr[i].length;j++){  
  23.                   
  24.                 arr[i][j]=j;  
  25.             }  
  26.               
  27.         }  
  28.           
  29.         //輸出  
  30.         for(int i=0;i<arr.length;i++){  
  31.               
  32.             for(int j=0;j<arr[i].length;j++){  
  33.                   
  34.                 System.out.print(arr[i][j]+" ");  
  35.             }  
  36.             System.out.println();  
  37.         }  
  38.           
  39.         /*輸出結果: 
  40.         0 1 2 3 4 5 6 7 8 9  
  41.         0 1 2  
  42.         0 1 2 3  
  43.         *///  
  44.           
  45.     }  
  46.       
  47. }  

 

 

 

[java] view plain copy  print?
  1. //3.編寫一個方法,返回double型二維數組,數組通過解析字串參數獲得。  
  2. //如"1,2;3,4,5;6,7,8"  
  3. //d[0,0]=1.0 d[0,1]=2.0 d[1,0]=3.0 ....  
  4. package me.parser;  
  5. public class TestString{  
  6.       
  7.     public static void main(String[] args){  
  8.           
  9.         //1.用字串分解split(";")成三個字串數組  
  10.         //2.再分解split(",")  
  11.           
  12.         //聲明一個二維數組用來裝分解好的字元  
  13.           
  14.           
  15.         String s="1,2;3,4,5;6,7,8";  
  16.         //以split()方法 分解  
  17.         String[] sFirst=s.split(";");  
  18.           
  19.         String[][] word=new String[sFirst.length][];  
  20.           
  21.         int flag=0;  
  22.         for(int i=0;i<sFirst.length;i++){  
  23.               
  24.             //列印出已經分開的  
  25.             //System.out.println(sFirst[i]);  
  26.             /*這條語句輸出 
  27.             1,2 
  28.             3,4,5 
  29.             6,7,8 
  30.             *///接下來在按照 ,分開他們放入一個一維數組  
  31.               
  32.             String[] sSecond=sFirst[i].split(",");  
  33.              //~ System.out.println(sSecond.length);  
  34.             //~ /*輸出:  
  35.             //~ 2  
  36.             //~ ---------------------------------  
  37.             //~ 3  
  38.             //~ ---------------------------------  
  39.             //~ 3  
  40.             //~ ---------------------------------             
  41.             //~ *///說明每次sSencond這個一維數組的長度不同  
  42.             word[i]=new String[sSecond.length];//這步確定行不規則數組的每行長度  
  43.       
  44.             //為這個數組賦值  
  45.             for(int j=0;j<sSecond.length;j++){  
  46.                       
  47.                 word[i][j]=sSecond[j];  
  48.             }  
  49.               
  50.             System.out.println("---------------這是第"+(i+1)+"次迴圈-------------------");  
  51.         }  
  52.           
  53.         //輸出二維數組  
  54.         System.out.println("輸出二維數組-------------------");  
  55.         for(int i=0;i<word.length;i++){  
  56.             for(int j=0;j<word[i].length;j++){  
  57.                   
  58.                 System.out.print(word[i][j]+" ");  
  59.             }  
  60.             System.out.println();  
  61.         }  
  62.         /*結果: 
  63.         ---------------這是第1次迴圈------------------- 
  64.         ---------------這是第2次迴圈------------------- 
  65.         ---------------這是第3次迴圈------------------- 
  66.         輸出二維數組------------------- 
  67.         1 2  
  68.         3 4 5  
  69.         6 7 8  
  70.         輸出二維數組------------------- 
  71.         *///  
  72.     }  
  73. }  

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.