數組(array)

來源:互聯網
上載者:User

標籤:car   static   ==   構造   main   print   read   靜態初始化   lan   

1 package cn.bjsxt.array;2 3 public class Car {4     String name;5     // 構造方法6     public Car(String name){7         this.name=name;8     }9 }
 1 package cn.bjsxt.array; 2 /** 3  * 數組 參考型別 你看成對象就行 4  * @author Administrator 5  *1.資料是相同資料類型(任何類型)集合 6  *2.數組也是對象,數組元素相當於對象的成員變數 7  *3.數組長度是確定的,不可變的。數組下標越界異常ArrayIndexOutOfBoundsException 8  * 9  */10 public class Test01 {11     public static void main(String[] args) {12         //只能放三個數13         int [] a = new int[3];14         a[0] = 1;15         a[1] = 2;16         a[2] = 3;17         //a[3] = 4;報錯 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 318         //            at cn.bjsxt.array.Test01.main(Test01.java:14)19         System.out.println(a.length);20         for(int i=0;i<a.length;i++){21             System.out.println(a[i]);22         }23         24         Car[]cars=new Car[4];25         cars[0]=new Car("平治");26     }27 }
 1 package cn.bjsxt.array; 2 /** 3  * 數組元素當成成員變數 4  * @author Administrator 5  * 6  */ 7 public class Test02 { 8     public static void main(String[] args) { 9         //聲明10         int[]a;11         //建立數組12         a=new int[4];13         //初始化    對數組元素賦值 有三種方式14         //1.預設初始化 和預設值和成員變數的規則一樣 數字 0    布爾 false char \u0000  引用 null15         16         //動態初始化17         for(int i=0;i<a.length;i++){18             a[i]=i*12;19         }20         21         //靜態初始化22         int c[]={12,12,425,758};//長度4  索引範圍[0,3]23         Car[]cars = {new Car("平治"),24                     new Car("寶馬"),25                     new Car("奧迪"),26                     new Car("別克")27                     };28         Car c1 = new Car("平治");29         System.out.println(c1==cars[0]);30         31     }32 }

 

數組(array)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.