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

          在java中,不能使用像int num[3];這樣的定義數組,在定義數組時不能分配空間的大小,只能通過為數組添加對象來分配空間。

          public class Welcome {

               public static void main(String[] args) {

                     int num[];

                  num=new int[3];

               }

           }

          或者在定義時直接定義大小。

          public class Welcome {

               public static void main(String[] args) {

                      int num[]=new int[3];

                      }

           }

          Java中建議使int[] num 這種方式這種方式定義了一個int類型的數組[],數組名稱為num。

          在定義時還可以直接初始化

          public class Welcome {

               public static void main(String[] args) {

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

                      }

           }

          但是這種方法只能在定義時進行。

          public class Welcome {

               public static void main(String[] args) {

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

                      }

           }

          還可以使用new int方法來定義,如上

          int[] num =new int[3]{1,2,3};這樣的寫法也是不允許的。不能分配空間大小。

          最簡單的方法是

          public class Welcome {

               public static void main(String[] args) {

                  int [] num;

                  num=new int[3];

                  num[0]=1;

                  num[1]=12;

                  num[2]=21;

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

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

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

                      }

           }

          posted on 2008-02-27 17:11 rick 閱讀(219) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 临高县| 通州市| 宁强县| 南投市| 彩票| 阿图什市| 额尔古纳市| 镇原县| 大冶市| 南开区| 盐亭县| 惠安县| 中牟县| 凭祥市| 灵山县| 合川市| 永川市| 南充市| 富顺县| 壤塘县| 仙居县| 德兴市| 沙河市| 白朗县| 栖霞市| 鸡西市| 高尔夫| 德阳市| 霍林郭勒市| 修武县| 闽侯县| 广平县| 蕉岭县| 石泉县| 临江市| 广饶县| 临桂县| 嵩明县| 雅安市| 呼图壁县| 汽车|