返回列表 發帖

陣列的長度

利用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 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. }
複製代碼

  1. public class Ch32
  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 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. }
複製代碼
我是眾神之王XXX  I love you
0000000000

TOP

  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. }
複製代碼

TOP

  1. public class Ch26
  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

返回列表