1.声明数组
数组的元素类型 数组名[]/数组的元素类型 [] 数组名;
例如:int[] arr={1,2,3};
int arr[]={1,2,3};
2.为数组分配元素
数组名=new 数组元素的类型[数组元素的个数];
类如:int[] arr=new int[5];
声明数组可以和 创建数组一起完成;
3.数组的访问
例:a[0]=100,a[1]=101;
4.数组遍历
import java.unit.Arrays;
public class Example{
public static void main(String[] args){
int[] a={1,2,3,4,5};
for(int n=0;n<a.length;n++){//传统方式遍历数组
System.out.println(a[n]+" “);
}
System.out.println();
for(int ch:a){//循环变量ch依次取数组a的每一个元素的值(非传统方式遍历)
System.out.println(ch+” ");
}
System.out.println();
System.out.println(Arrays.toString(a));
}
}