Board logo

標題: 陣列的長度 [打印本頁]

作者: tonyh    時間: 2012-11-3 16:42     標題: 陣列的長度

本帖最後由 tonyh 於 2012-11-3 16:46 編輯

利用length函數, 可算出陣列的長度, 即該陣列包含了幾位成員.
  1. public class ch39
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int a[]={1,2,3,4};
  6.         int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  7.         int c[][][]={{{1,2,3,4},{1,2,3,4},{1,2,3,4}},{{1,2,3,4},{1,2,3,4},{1,2,3,4}}};
  8.         System.out.println(a.length);    //4
  9.         System.out.println(b.length);    //3
  10.         System.out.println(c.length);    //2
  11.     }
  12. }
複製代碼

作者: t3742238    時間: 2012-11-3 16:45

  1. public class ch39
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int a[]={1,2,3,4};
  6.         int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  7.         int c[][][]={{{1,2,3,4},{1,2,3,4},{1,2,3,4}},{{1,2,3,4},{1,2,3,4},{1,2,3,4}}};
  8.         System.out.println(a.length);
  9.         System.out.println(b.length);
  10.         System.out.println(c.length);
  11.     }
  12. }
複製代碼

作者: 黃博鴻    時間: 2012-11-3 16:45

  1. public class ch39
  2. {
  3.    public static void main(String args[])
  4.    {
  5.       int a[]={1,2,3,4};
  6.       int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  7.       int c[][][]={{{1,2,3,4},{1,2,3,4},{1,2,3,4}},{{1,2,3,4}
  8.                     ,{1,2,3,4},{1,2,3,4}}};
  9.              System.out.println(a.length);
  10.              System.out.println(c.length);
  11.              System.out.println(b.length);
  12.    }
  13. }
複製代碼

作者: 劉漢文    時間: 2012-11-3 16:45

  1. public class ch39
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int a[]={1,2,3,4};
  6.         int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  7.         int c[][][]={{{1,2,3,4},{1,2,3,4},{1,2,3,4}},{{1,2,3,4},{1,2,3,4},{1,2,3,4}}};
  8.         System.out.println(a.length);
  9.         System.out.println(b.length);
  10.         System.out.println(c.length);
  11.     }
  12. }
複製代碼

作者: t2364705    時間: 2012-11-3 16:46

  1. public class ch39
  2. {
  3.    public static void main(String args[])
  4.    {
  5.       int a[]={1,2,3,4};
  6.       int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  7.       int c[][][]={{{1,2,3,4},{1,2,3,4},{1,2,3,4}},{{1,2,3,4},{1,2,3,4},{1,2,3,4}}};
  8.       System.out.println(a.length);
  9.       System.out.println(c.length);
  10.       System.out.println(b.length);
  11.    }
  12. }
複製代碼

作者: 尤泓鈞    時間: 2012-11-3 16:47

  1. public class ch39
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int a[]={1,2,3,4};
  6.         int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  7.         int c[][][]={{{1,2,3,4},{1,2,3,4},{1,2,3,4}},{{1,2,3,4},{1,2,3,4},{1,2,3,4}}};
  8.         System.out.println(a.length);
  9.         System.out.println(b.length);
  10.         System.out.println(c.length);
  11.     }
  12. }
複製代碼

作者: 蔡昀佑    時間: 2012-11-3 16:48

  1. public class ch38
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int a[][][]={{{1,2,3},{4,5,6},{7,8,9}},
  6.                      {{10,11,12},{13,14,15},{16,17,18}},
  7.                      {{19,20,21},{22,23,24},{25,26,27}}};
  8.         for(int i=0; i<=2; i++)
  9.         {
  10.             for(int j=0; j<=2; j++)
  11.             {
  12.                for(int k=0; k<=2; k++)
  13.                {
  14.                   System.out.println("a["+i+"]["+j+"]["+k+"]="+a[i][j][k]);
  15.                }
  16.             }
  17.         }
  18.     }
  19. }
複製代碼

作者: 粘靖瑜    時間: 2012-11-3 16:51

  1. public class ch39
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int a[]={1,2,3,4};
  6.         int b[][]={{1,2,3,4},{1,2,3,4}};
  7.         int c[][][]={{{1,2,3,4},{1,2,3,4},{1,2,3,4}},{{1,2,3,4},{1,2,3,4},{1,2,3,4}}};
  8.         System.out.println(a.length);
  9.         System.out.println(b.length);
  10.         System.out.println(c.length);

  11.     }
  12. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2