数组是存储变量的空间.
数组是一个变量,存储相同类型的一组数据.
一维数组的格式 数组基本要素 标识符 数组元素 元素下标:从 0 开始 元素类型三种格式
//数据类型[ ] 数组名 = new 数据类型[大小] ;
int[] a = new int[2];
//数据类型[] 数组名 = new 数据类型[]{内容 1,内容 2,内容 3…内容 n};
int[] b = new int[]{16,5,7,5};
//数据类型[] 数组名 ={内容 1,内容 2,内容 3…内容 n};
int[] c ={1,3,4,5,6,8};
数组元素根据类型不同,有不同的初始值
public class Demo4 {
public static void main(String[] args) {
//整型
byte[] a1=new byte[2];
short[] a2=new short[2];
int[] a3= new int[2];
long[] a4=new long[2];
//浮点型
float[] b1=new float[2];
double[] b2=new double[2];
//布尔型
boolean[] c1=new boolean[2];
//字符型
char[] d1=new char[2];
//字符串型
String[] e1=new String[2];
System.out.println(a1[0]);
System.out.println(a2[0]);
System.out.println(a3[0]);
System.out.println(a4[0]);
System.out.println(b1[0]);
System.out.println(b2[0]);
System.out.println(c1[0]);
System.out.println(d1[0]);
System.out.println(e1[0]);
}
}
增强for循环遍历
for(数据类型 变量名 :遍历的目标){ //数据类型 变量名:声明一个变量用来接收遍历目标遍历后的元素}
public class Demo5 {
public static void main(String[] args) {
int[] a= new int[]{12,44,557,88,33,88};
for (int b:
a) {
System.out.println(b);
}
}
}



