標籤: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)