返回列表 發帖

陣列的長度

利用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 Ch01 {
  2.         public static void main(String[] args) {
  3.                 int a[]={1,2,3,4};
  4.                 int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  5.                 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}}};
  6.                 System.out.println(a.length);
  7.                 System.out.println(b.length);
  8.                 System.out.println(c.length);
  9.         }
  10. }
複製代碼

TOP

  1. public class Ch1 {

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

TOP

  1. public class Ch30 {

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

TOP

  1. public class Ch37 {

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

TOP

  1. public class Ch01 {

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

TOP

  1. public class Ch50
  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.                
  14.                
  15.     }


  16.                
  17. }
複製代碼

TOP

  1. public class Ch01
  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. package po;

  2. public class po {

  3.         public static void main(String[] args) {
  4.     int a[]={1,2,3,4};
  5.     int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  6.     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}}};
  7.     System.out.println(a.length);
  8.     System.out.println(b.length);
  9.     System.out.println(c.length);
  10.         }

  11. }
複製代碼

TOP

  1. import java.util.Scanner;


  2. public class Ch54 {

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

TOP

  1. public class Ch30 {

  2.         public static void main(String[] args)
  3.         {
  4. int a[]={1,2,3,4};
  5. int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  6. 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}}};
  7. System.out.println(a.length);
  8. System.out.println(b.length);
  9. System.out.println(c.length);

  10.         }
  11. }
複製代碼

TOP

  1. public class Ch4 {

  2.         public static void main(String[] args)
  3.         {
  4.         int a[]={1,2,3,4};
  5.         int b[][]={{1,2,3,4,},{1,2,3,4},{1,2,3,4}};
  6.         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}}};
  7.         System.out.print(a.length);
  8.         System.out.println(b.length);
  9.         System.out.print(c.length);
  10.         }

  11. }
複製代碼

TOP

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

返回列表