返回列表 發帖

陣列的長度

利用length屬性, 可取得陣列的長度, 即該陣列包含了幾位成員.

int a[]={1,2,3,4};
int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
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}}};


上列三個陣列長度分別為何呢?
  1. public class Ch41
  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. }
複製代碼

  1. public class OwO
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int x[]={1,2,3,4};
  6.         int y[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
  7.         int z[][][]={{{1,2,3,4},{5,6,7,8},{9,10,11,12}},{{13,14,15,16},{17,18,19,20},{21,22,23,24}}};
  8.         System.out.println(x.length);     
  9.         System.out.println(y.length);     
  10.         System.out.println(z.length);     
  11.     }
  12. }
複製代碼

TOP

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

TOP

  1. public class hg
  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. }
複製代碼

TOP

  1. public class a03
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int x[]={1,2,3,4};
  6.         int y[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  7.         int z[][][]={{{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(x.length);
  9.         System.out.println(y.length);
  10.         System.out.println(z.length);
  11.     }
  12. }
複製代碼

TOP

  1. public class Ch66
  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. }
複製代碼

TOP

  1. public class Ch666
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         int a[]={1,2,3,4};
  6.         int s[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  7.         int d[][][]={{{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(s.length);
  10.         System.out.println(d.length);
  11.     }
  12. }
複製代碼

TOP

  1. public class A01
  2. {
  3.        public static void main(String  args[])
  4.        {
  5.        int a[]={1,2,3,4};
  6.        int b[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
  7.        int c[][][]={{{1,2,3,4},{5,6,7,8},{9,10,11,12}},
  8.        {{12,11,10,9},{8,7,6,5,},{4,3,2,1}}};

  9.        System.out.println(a.length);
  10.        System.out.println(b.length);
  11.        System.out.println(c.length);
  12.        }
  13. }
複製代碼

TOP

  1. public class Ch1
  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);
複製代碼

TOP

返回列表