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

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


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


          網站導航:
           
          主站蜘蛛池模板: 江西省| 玉门市| 进贤县| 孝昌县| 汶上县| 平潭县| 丹凤县| 彭泽县| 墨竹工卡县| 皋兰县| 北票市| 峡江县| 金华市| 江西省| 徐州市| 大关县| 通河县| 中牟县| 斗六市| 唐海县| 嘉兴市| 鄂伦春自治旗| 白朗县| 宽甸| 任丘市| 灵台县| 夏津县| 连云港市| 东乡| 印江| 新干县| 囊谦县| 海伦市| 类乌齐县| 宁阳县| 岑巩县| 固阳县| 涟水县| 太康县| 邵阳县| 神木县|