1.定义
在计算机科学中,数组是有一组元素(值或变量)组成的数据结构,每个元素有至少一个索引或健来标识。
2.初始化
(1)一维
<1>动态数组
数据类型 [ ] 数组名称 = new 数据类型[数组长度]
int[] arr = new int[5];
<2>静态数组:值已固定,无法改变
数据类型 [ ] 数组名称 = new 数据类型[ ]{元素1,元素2,元素3…}
int[] arr = new int[]{1,2,3};
数据类型 [ ] 数组名称 = {元素1,元素2,元素3…}
int[] arr = {1,2,3};
(2)二维
<1>方法一
int[][] arr = new int[5][3];
int arr = new int[5][3];
<2>方法二
int[] arr[] = new int[4][];
arr[0] = new int[5];
arr[1] = new int[2];
arr[2] = new int[7];
arr[3] = new int[9];
<3>方法三
int[][] arr = { {1, 2, 3}, {4,5,6}, {7,8,9,10} };
数组的基本类型:int, boolean, char, long, float, double, short, byte
3.常用方法
(1)length(): 获得数组的长度
int[] arr={1,5,6,8,2,3,7};
System.out.println(arr.length);//7
(2)equals():判断两个数组值和内存是否相等
int[] arr1 = {123};
int[] arr2={123};
System.out.println(arr1.equals(arr2));//false 因为这两个数组没有指向同一个内存地址
int[] arr3=arr1;
System.out.println(arr1.equals(arr3));//true
(3)Arrays.equals():判断两个数组值是否相等
int[] arr1 = {123};
int[] arr2={123};
System.out.println(Arrays.equals(arr1,arr2));//true
(4)toString(),Arrays.toString():返回数组的字符串表示形式
int[] arr = {1};
String arrString = Arrays.toString(arr);
System.out.println(arr);// [I@722c41f4 直接输出,为内存地址
System.out.println(arrString );// [1]
(5)Arrays.parallelSort(): 将数组排序
int[] a = {1,8,51,13,46,11,22};
Arrays.parallelSort(a);
System.out.println(Arrays.toString(a));//[1, 8, 11, 13, 22, 46, 51]
(6)Arrays.sort() 方法: 按照数字顺序排列指定的数组
//作用一
int[] arr1={1,9,3,7};
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));//[1, 3, 7, 9]
//作用二
int[] arr2={1,9,3,7,2,5};
Arrays.sort(arr2,1,4);//范围包含1不包4
System.out.println(Arrays.toString(arr2));//[1, 3, 9, 7, 2, 5]
(7)Arrays.asList(): 输出数组,或判断数组中是否有指定字符串
String[] arr={"4","2","3"};
//作用一
System.out.println(Arrays.asList(arr));//[4, 2, 3]
//作用二
System.out.println(Arrays.asList(arr).contains("2"));//true
(8)Arrays.binarySearch(): 查找某个值在算法中的位置,必须是有序数组
int[] a = {1,2,3,4,5,6};
System.out.println(Arrays.binarySearch(a,3));//2
System.out.println(Arrays.binarySearch(a,1,5,3));//2 对查询的东西指定范围(1,5)为范围
(9)clone():创建并返回此对象的副本
int [] arr = {7, 8, 9};
int [] cloneArr = arr.clone();//调用数组的clone()方法,将数组复制一份
cloneArr[2] = 888;
System.out.println(Arrays.toString(arr));//[7, 8, 9]
System.out.println(Arrays.toString(cloneArr));//[7, 8, 888]
(10)Arrays.copyOf() :从第一位开始复制指定长度的大小
int [] arr = {1,9,3,7,4,4,5};
int [] arr1=Arrays.copyOf(arr,6);
System.out.println(Arrays.toString(arr1));//[1, 9, 3, 7, 4, 4]
(11) Arrays.copyOfRange(): 将数组的指定范围复制到新数组中
int [] arr = {1,9,3,7};
int [] arr2=Arrays.copyOfRange(arr,1,3);//范围包含1不含3
System.out.println(Arrays.toString(arr2));//[9, 3]
(12)System.arraycopy():从某一数组指定位置开始 复制到另一数组的指定位置 并限制长度
int [] arr = {1,9,3,7};
int [] arr1 = new int[6];
System.arraycopy(arr,1,arr1,0,3);
//从arr的第1位开始,复制到arr1的第0位,复制3位数,不够的用0补
System.out.println(Arrays.toString(arr1)); //[9, 3, 7, 0, 0, 0]
(13)Arrays.deepEquals(): 判断数组深度,相等返回 true
int a[][] = new int[][]{{0}, {1, 2, 3, 5, 4}};
int b[][] = new int[][]{{0}, {1, 2, 3, 5, 4}};
int c[][] = new int[][]{{0}};
System.out.println(Arrays.deepEquals(a, b));//deepEquals适用于多层次,true
System.out.println(Arrays.deepEquals(a, c));//false
(14)Arrays.fill(): 将某值分配给数组的每个元素
int[] arr=new int[3];
Arrays.fill(arr,6);
System.out.println(Arrays.toString(arr));//[6, 6, 6]
(15)hashCode(),Arrays.hashCode():根据指定数组的内容返回哈希码
int[] arr1 = {1};
System.out.println(arr1.hashCode());//1300109446
System.out.println(Arrays.hashCode(arr1));//32
(16)getClass():返回此 Object的运行时类
int a[][] = {{1},{2}};
System.out.println(a.getClass());//class [[I
System.out.println(a.getClass().getName());//[[I 几维数组就有几个[