1、字符串數組
//定義字符串數組
String[] s=new String[6];
//定義字符串數組並賦值
String[] str=new String[]{"a","b","c"};
String[] str2={"a","b","c"}
2、整形數組
//定義整型數組
int[] i=new int[6];
//定義整型數組並賦值
int[] ii=new int[]{0,1,2,3};
3、測試
package day03; public class TestArray { public static void main(String [] args) { int [] b; b = new int[] {88,99,66}; //分步定義數組,先定義數組名,然后再為數組賦值 int [] d = {88,99,100}; //直接定義數組,同時賦值 System.out.println(d[0]);//訪問數組的元素,需要通過 數組名[元素下標] 來訪問 // System.out.println(d[3]);//錯誤的演示,如果訪問的下標超過了數組的最大下標,編譯不會報錯,但是執行會報錯 int [] c = new int[3]; //只定義數組元素的個數,沒有為其賦值。 System.out.println(c); //打印數組名,打印出來的是數組再堆內存中的地址 //數組都是有默認值的,boolean:false ,String:null。 boolean [] bl = new boolean[3]; System.out.println(bl[0]); String [] s = new String [3]; System.out.println(s[0]); double [] dou = new double[3]; System.out.println(dou[0]); System.out.println(c[0]); c[0]=1000; //可以通過數組下標的方式為數組賦值 System.out.println(c[0]); } }
結果展示:
88 [I@7852e922 flase null 0.0 1000