返回列表 發帖

陣列的長度

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

TOP

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

TOP

  1. public class Ch38
  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 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);
  9.         System.out.println(b.length);
  10.         System.out.println(c.length);
  11.     }
  12. }
複製代碼

TOP

  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);   
  9.         System.out.println(b.length);   
  10.         System.out.println(c.length);   
  11.     }
  12. }
複製代碼

TOP

  1. public class Ch37
  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 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)
  9.         System.out.println(b.length)
  10.         System.out.println(c.length)
  11.     }
  12. }
複製代碼

TOP

  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);
  9.     System.out.println(b.length);
  10.     System.out.println(c.length);     
  11.   
  12.   }
  13. }
複製代碼

TOP

  1. public class Ch18
  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,lenght);
  9.         System.out.println(b,lenght);
  10.         System.out.println(c,lenght);
  11.     }
  12. }
複製代碼

TOP

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

返回列表