1.數(shù)組(Array):相同類型數(shù)據(jù)的集合就叫做數(shù)組。
2. 數(shù)組的定義與賦值(系統(tǒng)會默認初始化)
普通數(shù)組:
1 package com.li; 2 3 public class Array{ 4 5 public static void main(String[] args) { 6 // TODO 自動生成的方法存根 7 int[] a=new int[2]; 8 9 /*10 * int a[]=new int[2];11 * int[] a={0,1}; 12 * int[] a=new int[]{0,1}; //type[] 變量名 = {new type[]}{逗號分隔的初始化值列表};13 */14 a[0]=0;15 a[1]=1;16 System.out.PRintln(a[0]);17 System.out.println(a[1]);18 19 System.out.println("---------------------------------------");20 /*錯誤的方法21 int[] b = null;22 for(int i=0;i<10;i++){23 b[i]=i;24 System.out.println(b[i]);25 }26 */27 28 29 }引用數(shù)組:
1 public class Array{ 2 3 public static void main(String[] args) { 4 Person[] p=new Person[3]; 5 //數(shù)組內(nèi)存放的是應用數(shù)據(jù)類型,指向?qū)ο螅跏紴閚ull 6 for(int i=0;i<p.length;i++){ 7 System.out.println(p[i]); 8 } 9 System.out.println("---------------------------------------");10 for(int i=0;i<p.length;i++){11 p[i]=new Person(i*10);12 System.out.println(p[i].age);13 }14 };15 }16 class Person{17 int age;18 public Person(int age){19 this.age=age;20 }21 }3.java中的每個數(shù)組都有一個名為length的屬性,表示數(shù)組的長度。length屬性是public,final,int的。數(shù)組長度一旦確定,就不能改變大小。
4.二維數(shù)組。二維數(shù)組是一種平面的二維結(jié)構(gòu),本質(zhì)上是數(shù)組的數(shù)組。二維數(shù)組的定義方式:type[][]a=new type[2][3];
新聞熱點
疑難解答