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

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

          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類型的數(shù)組[],數(shù)組名稱為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)  編輯  收藏


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 永仁县| 道真| 张家港市| 阳西县| 玉林市| 武威市| 莆田市| 百色市| 化州市| 远安县| 四川省| 盖州市| 墨江| 博兴县| 南平市| 巴彦淖尔市| 三门峡市| 雷州市| 绥棱县| 万源市| 扎赉特旗| 堆龙德庆县| 韶山市| 晋中市| 偃师市| 梓潼县| 营山县| 西藏| 霍州市| 南靖县| 荣成市| 新津县| 措勤县| 永善县| 英吉沙县| 武乡县| 辽源市| 交口县| 仁怀市| 深圳市| 根河市|