返回列表 發帖

陣列的長度

利用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 Ch11
  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 Ch13
  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.                                      {{13,14,15,16},{17,18,19,20},{21,22,23,24}}};
  9.                 System.out.println(a.length);//4
  10.                 System.out.println(b.length);//3
  11.                 System.out.println(c.length);//2

  12.         }

  13. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch16
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         int a[]={1,2,3,4};
  7.         int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  8.         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}}};
  9.         System.out.println(a.length);     
  10.         System.out.println(b.length);     
  11.         System.out.println(c.length);   
  12.     }
  13. }
複製代碼

TOP

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

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

TOP

返回列表