数组
相同类型数据的集合就叫做数组
数组的定义
方式一:
type[] 变量名 = new type[数组中元素的个数];前者type[]是类型,不能填写个数。后面才是开辟对象空间需要指定个数。
方式二:
type 变量名[] = new type[数组中元素的个数];
方式三:
int[] a = new int[]{1,2,3,4};//初始值 不能指定个数
PS:int[] a = new int[10],其中a是一个引用,它指向了生成的数组对象的首地址,数组中每个元素都是int 类型,其中仅存放数据值本身。如果数组是对象型数组,那数组装的是引用。
数组在内存中是连续存放的,所以可以直接根据下标读取。
数组调用equsl对比的是对象,会不同,没重写object的equsl
数组的赋值
方式一:
int[] a = new int[4]; a[0]=1;a[1]=2……
方式二:
方式二:
int[] a ={1,2,3,4}
方式三:
方式三:
int[] a = new int[]{1,2,3,4};//初始值 不能指定个数
二维数组
方式一:
type[][] a = new type[2][3];
方式二:
type[][] a = new type[2][];
a[0]=new type[4];
a[1]=new type[5];
方式三:
type[][] a = new type[][]{{1,2,2},{4,5},{5,4,1}};
type[][] a = new type[2][3];
方式二:
type[][] a = new type[2][];
a[0]=new type[4];
a[1]=new type[5];
方式三:
type[][] a = new type[][]{{1,2,2},{4,5},{5,4,1}};
相关工具
数组工具类:arrays