數據加載中……
          二維數組的使用

          Java中,二維數組的定義和一維數組是一樣的。

          public class Welcome {

               public static void main(String[] args) {

                  int [][] num;

                  num=new int[3][4];

                  num[0][1]=1;

                  num[1][3]=12;

                  num[2][2]=21;        

                      }

          }

          Java中二維數組的列數可以不同,比如第一行是2列,第二行是4列,等等。

          有些類似與c語言中的指針數組。

          public class Welcome {

               public static void main(String[] args) {

                   int [][] num;

                  num=new int[3][];

                  num[0]=new int[2];

                  num[1]=new int[4];

                  num[2]=new int[3];

                  num[0][1]=12;

                  num[1][3]=23;

                  num[2][2]=34;

                  System.out.println(num[0][1]);

                  System.out.println(num[1][3]);

                  System.out.println(num[2][2]);

                      }

           }

          在java中沒有定義的自動賦值為0.

          在定義時可以為二維數組來定義,但是在賦初值時 ,不要在定義大小了

          int [][] num=new int[3][]{1,2,3,4,5,2,3,4,5,6} //error

          public class Welcome {

               public static void main(String[] args) {

                   int [][] num=new int[][]{1,2,3,4,5,2,3,4,5,6}; //error

                   int [][] num=new int[][]{{1,2,3,4,5},{2,3,4,5,6}}; //ok    

                  }

          }

          定義二維數組還有一種方式

          public class Welcome {

               public static void main(String[] args) {

                     int [][] num={1,2,3,4,5,2,3,4,5,6}; //error

                   }

          }

          也可以定義不規則的二維數組

          public class Welcome {

               public static void main(String[] args) {

                   int [][] num={{1,2,3},{4,5},{2}};

                  System.out.println(num[0][2]);

                  System.out.println(num[1][1]);

                  System.out.println(num[2][0]);

                   }

          }

          posted on 2008-02-28 11:00 rick 閱讀(3339) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 汕头市| 禹州市| 砚山县| 乌什县| 万安县| 乳源| 武强县| 龙泉市| 龙井市| 上林县| 郴州市| 修水县| 增城市| 云安县| 登封市| 永定县| 襄垣县| 板桥市| 岑巩县| 桑植县| 绥芬河市| 中宁县| 普安县| 利津县| 台北县| 西峡县| 乐都县| 惠来县| 秦皇岛市| 叙永县| 静乐县| 平顶山市| 固阳县| 福建省| 忻城县| 上思县| 喜德县| 佛山市| 英吉沙县| 曲阳县| 宾阳县|