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

          在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 閱讀(217) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 同心县| 神农架林区| 泸水县| 克什克腾旗| 万全县| 罗定市| 榕江县| 英德市| 会宁县| 察隅县| 璧山县| 昌乐县| 桦南县| 广西| 上高县| 邯郸市| 翁源县| 镇宁| 阳信县| 武隆县| 林西县| 龙山县| 禄丰县| 乌兰察布市| 汝城县| 韩城市| 奇台县| 东山县| 凤山县| 江永县| 奉节县| 衡阳县| 宁乡县| 孟州市| 西峡县| 崇礼县| 灵台县| 灌阳县| 丰镇市| 丘北县| 通山县|