数组的定义
数组:是相同类型数据的有序集合。其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们
数组的声明与创建
-
数组的声明:
- dataType[ ] arrayName; (推荐)
- dataType arrayName[ ]; (不推荐)
- 举例
- public static void main(String[ ] args) { } (推荐)
- public static void main(String args[ ]) { } (不推荐)
-
数组的创建:arrayName = new dataType[ arraySize ];
-
数组的声明与创建:dataType[ ] arrayName = new dataType[ arraySize ];
-
数组的长度:array.length
-
数组的合法区间:数组的合法区间:[0, length-1],越界就会报错
-
内存分析
1.声明数组
-
创建数组
-
给数组元素赋值
-
-
// 数组的声明与创建 public class Array01 { public static void main(String[] args) { // 声明数组 int[] array; // 创建数组 array = new int[5]; // 声明并创建 int[] arrays = new int[5]; int[] arr = new int[]{1, 2, 3, 4, 5}; } }
-
// 数组的赋值与长度 public class Array02 { public static void main(String[] args) { // 声明并创建一个长度为5的数组array 数组一旦被创建,大小不可改变 int[] array = new int[5]; // 给数组赋值 数组的元素是通过索引访问的,数组索引从0开始 array[0] = 1; // 给数组array的第1个数赋值1 array[1] = 2; // 给数组array的第2个数赋值2 array[2] = 3; // 给数组array的第3个数赋值3 array[3] = 4; // 给数组array的第4个数赋值4 array[4] = 5; // 给数组array的第5个数赋值5 // 数组的合法区间:[0, length-1],越界就会报错 // 错误原因:因为array数组的长度为5,array[5]是第六个元素,导致数组下标越界 // array[5] = 5; // 报错:ArrayIndexOutOfBoundsException 数组下标越界 // 返回数组的长度 array.length System.out.println(array.length); // 5 } }
数组的初始化
-
静态初始化:
- dataType[ ] arrayName = new dataType[ ]{ 值, 值, 值, … }; (完整)
- dataType[ ] arrayName = { 值, 值, 值, … }; (简写)
-
动态初始化:dataType[ ] arrayName = new dataType[ arraySize ];
-
// 数组的初始化 public class Array03 { public static void main(String[] args) { // 静态初始化 int[] a = {1, 2, 3, 4, 5}; // 动态初始化 int[] b = new int[5]; // 动态初始化 —— 默认初始化 b[0] = 1; System.out.println(b[0]); // 1 // 因为b[1]没有给它赋值,会隐式初始化,int类型的默认值为0 System.out.println(b[1]); // 0 } }
数组的使用
-
数组的常见题目
// 数组的常见题目 public class Array04 { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; int[] arr = new int[]{1,2,34}; System.out.println(array); //[I@4554617c // for打印数组 for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } // foreach打印数组 for (int i : array) { System.out.println(i); } // 计算所有元素的和 int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } System.out.println("sum=" + sum); // 查找最大元素 int max = 0; for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } System.out.println("max=" + max); // 反转数组 int[] result = new int[array.length]; for (int i = 0, j = result.length - 1; i < array.length; i++, j--) { result[j] = array[i]; } System.out.println(Arrays.toString(result)); } }
-
二维数组与三维数组的创建与打印
// 二维数组与三维数组的创建与打印 public class Array05 { public static void main(String[] args) { // 二维数组 int[][] array = new int[3][2]; array[0][0] = 1; array[0][1] = 2; array[1][0] = 3; array[1][1] = 4; array[2][0] = 5; array[2][1] = 6; int[][] arrays = {{1, 2}, {3, 4}, {5, 6}}; // 三维数组 int[][][] arr = {{{1, 2, 3}, {1, 2, 3}}, {{4, 5, 6}, {4, 5, 6}}, {{7, 8, 9}, {7, 8, 9}}}; // 打印二维数组 for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.println(array[i][j]); } } // 打印三维数组 for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { for (int k = 0; k < arr[i][j].length; k++) { System.out.println(arr[i][j][k]); } } } } }
-
Arrays类返回数组的方法
- Arrays.asList() 返回由指定数组支持的固定大小的列表。(注意:只能对Object数组有效)
- Arrays.toString() 返回指定数组的内容的字符串表示形式。(适用于一维数组)
- Arrays.deepToString() 返回指定数组的“深度内容”的字符串表示形式。(适用于二维数组及以上)
// Arrays类 返回数组的方法 public class Array06 { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; int[][] arrays = {{1, 2}, {3, 4}, {5, 6}}; Integer[] arr = {1, 2, 3}; // Arrays.asList() 返回由指定数组支持的固定大小的列表。 System.out.println(Arrays.asList(arr)); // [1, 2, 3] System.out.println(Arrays.asList(array)); // [[I@4554617c] // Arrays.toString() 返回指定数组的内容的字符串表示形式。 System.out.println(Arrays.toString(array)); // [1, 2, 3, 4, 5] // Arrays.deepToString() 返回指定数组的“深度内容”的字符串表示形式。 System.out.println(Arrays.deepToString(arrays)); // [[1, 2], [3, 4], [5, 6]] } }
-
Arrays类其他常用方法
-
binarySearch() 使用二叉搜索算法搜索指定的int数组的指定值。
-
equals() 如果两个指定的int数组彼此 相等 ,则返回 true。
-
fill() 将指定的int值分配给指定的int数组的每个元素。
-
fill(int[] a, int fromIndex, int toIndex, int val) 将指定的int值分配给指定的int数组的指定范围的每个元素。
(包含fromIndex,不包含toIndex,val为指定范围所要分配的值,val默认为0)
-
sort() 按照数字顺序排列指定的数组。 升序
-
sort(int[] a, int fromIndex, int toIndex)
(按升序排列数组的指定范围。包含fromIndex,不包含toIndex) -
parallelSort(int[] a) 按照数字顺序排列指定的数组。
-
parallelSort(int[] a, int fromIndex, int toIndex) 按照数字顺序排列数组的指定范围。
(包含fromIndex,不包含toIndex) -
parallelSort()与 sort()比较
- 当我们要排序的数据集很大时,parallelSort() 可能是更好的选择。
- 但是,在数组较小的情况下,最好使用 sort(),因为它可以提供更好的性能。
// Arrays类 其他常用方法 public class Array07 { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int[] arrs = {1, 2, 3, 4, 5}; int[] array = {1, 3, 2, 5, 4}; int[] arrays = {1, 3, 2, 5, 4}; int[] arrayss = {1, 3, 2, 5, 4}; int[] arraysss = {1, 3, 2, 5, 4}; // Arrays.binarySearch() 使用二叉搜索算法搜索指定的int数组的指定值。 System.out.println(Arrays.binarySearch(arr,2)); // 1 // Arrays.equals() 如果两个指定的int数组彼此 相等 ,则返回 true。 System.out.println(Arrays.equals(arr,array)); // false // fill() 将指定的int值分配给指定的int数组的每个元素。 Arrays.fill(arr,0); System.out.println(Arrays.toString(arr)); // [0, 0, 0, 0, 0] // fill(int[] a, int fromIndex, int toIndex, int val) 将指定的int值分配给指定的int数组的指定范围的每个元素。 Arrays.fill(arrs,1,3,0); System.out.println(Arrays.toString(arrs)); // [1, 0, 0, 4, 5] // Arrays.sort() 按照数字顺序排列指定的数组。 升序 Arrays.sort(array); System.out.println(Arrays.toString(array)); // [1, 2, 3, 4, 5] // sort(int[] a, int fromIndex, int toIndex) Arrays.sort(arrays,1,3); System.out.println(Arrays.toString(arrays)); // [1, 2, 3, 5, 4] // parallelSort(int[] a) 按照数字顺序排列指定的数组。 Arrays.parallelSort(arrayss); System.out.println(Arrays.toString(arrayss)); // [1, 2, 3, 4, 5] // parallelSort(int[] a, int fromIndex, int toIndex) 按照数字顺序排列数组的指定范围。 Arrays.parallelSort(arraysss,1,3); System.out.println(Arrays.toString(arraysss)); // [1, 2, 3, 5, 4] } }
-
备注
如果您想了解数组的基础知识,可以参考我的另一篇文章。Java基础笔记
created by XQ_JOKER in 2021/4/20
updated by XQ_JOKER in 2021/4/25