數(shù)據(jù)加載中……
          二維數(shù)組的使用

          Java中,二維數(shù)組的定義和一維數(shù)組是一樣的。

          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中二維數(shù)組的列數(shù)可以不同,比如第一行是2列,第二行是4列,等等。

          有些類似與c語言中的指針數(shù)組。

          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.

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

          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    

                  }

          }

          定義二維數(shù)組還有一種方式

          public class Welcome {

               public static void main(String[] args) {

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

                   }

          }

          也可以定義不規(guī)則的二維數(shù)組

          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)  編輯  收藏


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 鹤山市| 东山县| 锡林浩特市| 石柱| 乌鲁木齐县| 若尔盖县| 延安市| 峨眉山市| 阿鲁科尔沁旗| 中宁县| 万山特区| 武鸣县| 东乡族自治县| 犍为县| 伊川县| 永登县| 平泉县| 郎溪县| 闵行区| 盱眙县| 广宁县| 理塘县| 延吉市| 定兴县| 泽州县| 金沙县| 甘洛县| 象山县| 台东市| 曲靖市| 海安县| 夏河县| 鄄城县| 江西省| 阿城市| 明光市| 北碚区| 嘉义市| 焦作市| 通江县| 辽源市|