Java 数组常用操作
2022-01-10 本文已影响0人
yjtuuige
数组初始化:
- 数组初始化后,才能使用。
- 为数组元素分配空间,并为数组中的每个元素赋值。
- 初始化分类:
- 静态初始化:指定长度,由系统赋初始值。
int[] arr= new int[]{1,2,3,4,5} // 简化 int[] arr2 ={10,20,30.40,50}
- 动态初始化:创建并赋值,由系统计算长度。
int[] arr=new int[5]
- 静态初始化:指定长度,由系统赋初始值。
数组常见操作
- 数组元素的遍历
package com.xxx.arrays; public class Demo01 { public static void main(String[] args) { // 静态初始化 int[] array = {10, 20, 30, 40, 50}; // 遍历数组 // array.length 数组长度(索引从 0 开始,最后索引为 array.length-1) for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } }
- 数组常见的角标越界异常
// ArrayIndexOutOfBoundsException:数组角标越界异常 //(最大索引值为 array.length-1,超出后出现异常) for (int i = 0; i <= array.length; i++) { System.out.println(array[i]); }
- 数组元素的反向遍历
package com.xxx.arrays; public class Demo02 { public static void main(String[] args) { int[] array = {10, 20, 30, 40, 50}; // 数组的反向遍历 // array.length - 1:最大索引值 i--:从最大索引值自减 for (int i = array.length - 1; i >= 0; i--) { System.out.println(array[i]); } } }
- 获取数组中元素的最大值或最小值
package com.xxx.arrays; public class Demo03 { public static void main(String[] args) { int[] array = {20, 10, 40, 30, 50}; // 获取数组中元素的最大值 // 定义一个参照值(第一个元素,索引 0) int max = array[0]; // 用后一个元素和前一个元素比较,查找最大值,保存到 max for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } System.out.println("数组最大值:" + max); // 获取数组中元素的最小值 // 定义一个参照值(第一个元素,索引 0) int min = array[0]; // 用后一个元素和前一个元素比较,查找最小值,保存到 min for (int i = 1; i < array.length; i++) { if (array[i] < min) { min = array[i]; } } System.out.println("数组最小值:" + min); } }
- 数组元素的反转
package com.xxx.arrays; import java.util.Arrays; public class Demo04 { public static void main(String[] args) { int[] array = {10, 20, 30, 40, 50}; // 反转数组中的元素 // 思路:元素首尾互换 for (int i = 0; i < array.length / 2; i++) { // temp:临时变量 array[i]:首元素(自增) array.length - 1 - i:尾元素(自减) int temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } // System.out.println(Arrays.toString(array)); // 遍历数组 for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } }
二维数组
- 二维数组格式
package com.xxx.arrays; public class Demo05 { public static void main(String[] args) { // 动态初始化二维数组(包含 3 个一维数组,每个一维数组的长度是 2) // 可以看成 3 行 2 列 int[][] array = new int[3][2]; // 静态初始化 int[][] array2 = new int[][]{{1, 2}, {3, 4}, {5, 6}}; // 简化 int[][] array3 = {{1, 2}, {3, 4}, {5, 6}}; // 取数组元素 // array3[1][1]:第二个一维数组的第二个元素(第一个索引为 0) System.out.println(array3[1][1]); // 其它方式定义(一个一维数组 a,和一个二维数组 b) int[] a, b[]; a = new int[1]; b = new int[2][2]; } }
- 二维数组遍历
package com.xxx.arrays; public class Demo06 { public static void main(String[] args) { int[][] array = {{1, 2, 5}, {3, 4, 2}, {5, 6}}; // 二维数组的遍历 // 外循环:遍历二维数组中的一维数组 for (int i = 0; i < array.length; i++) { // 内循环:遍历一维数组中的每个元素 for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j] + " "); } // 每打印完一个一维数组后,换行 System.out.println(); } } }
- 实例 1:
package com.xxx.arrays; public class Demo07 { public static void main(String[] args) { // 统计数据之和 // 第一季度:22,33,55 // 第二季度:13,28,65 // 第三季度:66,22,18 // 第四季度:30,60,77 // 创建数组 int[][] array = {{22, 33, 55}, {13, 28, 65}, {66, 22, 18}, {30, 60, 77}}; // 定义临时变量 int sum = 0; // 遍历二维数组,并计算累加之和 for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { sum += array[i][j]; } } System.out.println("总和:" + sum); } }
- 实例 2(杨辉三角):
- 每一行的第一列和最后一列都是 1。
- 从第三行开始,从第二列开始,这个数字等于他上一行的前一列,和上一行的本列之和。
package com.xxx.arrays; import java.util.Scanner; public class Demo08 { public static void main(String[] args) { // 需求:通过输入行数,打印出杨辉三角 /* 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 */ // 创建 Scanner 对象,接收输入信息 Scanner sc = new Scanner(System.in); System.out.println("请输入行数:"); int n = sc.nextInt(); // 定义二维数组,存储数据 int[][] array = new int[n][n]; // 1.设置最外边的值(1) // 把每行的第一列,和每行的最后一列设置为 1 for (int i = 0; i < array.length; i++) { array[i][0] = 1; array[i][i] = 1; } // 2.设置中间值 // 从第三行开始,从第二列开始,这个数字等于 // 他上一行的前一列,和上一行的本列之和 // 外循环:i = 2 第三列开始 for (int i = 2; i < array.length; i++) { // 内循环:j = 1 第二列开始 for (int j = 1; j <= i; j++) { // array[i-1][j-1]:上一行的前一列 array[i-1][j]:上一行的本列 array[i][j]=array[i-1][j-1]+array[i-1][j]; } } // 3.遍历输出 for (int i = 0; i < array.length; i++) { for (int j = 0; j <= i; j++) { System.out.print(array[i][j] + "\t"); } System.out.println(); } // 关闭 Scanner sc.close(); } }
查找元素索引
-
方式一:遍历数组挨个查找
package com.xxx.arrays; public class Demo09 { public static void main(String[] args) { int[] array = {90, 10, -5, 20, 88, 40, 12, 30, 20, 50}; // 查找元素在数组中第一次出现的索引 int index = getIndex(array, 20); System.out.println("该元素的第一次索引:" + index); } private static int getIndex(int[] array, int ele) { // 遍历数组 for (int i = 0; i < array.length; i++) { if (ele == array[i]) { return i; } } return -1; // 如果没找到返回 -1 } }
-
方式二:二分查找
- 二分查找前提:数组元素必须有序。
- 二分查找思想:每次查找中间元素,通过比较大小,能减少一半元素的查找,可提高效率。
- 通过与中间值比较,比较后三种情况:
- 查找的元素,正好是中间索引(mid)对应元素,返回索引值;
- 查找的元素,小于中间元素,改变最大索引位置,为中间索引的前一个索引(mid-1),继续查找,直到最大索引(max)和最小索引(min)重合;
- 查找的元素,大于中间元素,改变最小索引位置,为中间索引的后一个索引(mid+1),继续查找,直到最小索引(min)和最大索引(max)重合;
package com.xxx.arrays; public class Demo10 { public static void main(String[] args) { // 二分查找:前提,数组必须是有序 int[] array = {10, 20, 30, 40, 50, 60, 70, 80}; int index = getIndex(array, 40); System.out.println("元素出现的索引:" + index); } private static int getIndex(int[] array, int ele) { // 最小索引 int min = 0; // 最大索引 int max = array.length - 1; // 中间索引:(最小+最大)/2 int mid = (min + max) / 2; while (min <= max) { // 等于中间索引,返回中间索引 if (ele == array[mid]) { return mid; // 小于中间索引,移动最大索引到中间索引-1 } else if (ele < array[mid]) { max = mid - 1; // 大于中间索引,移动最小索引到中间索引+1 } else if (ele > array[mid]) { min = mid + 1; } // 重新计算中间索引值 mid = (min + max) / 2; } return -1; // 找不到返回 -1 } }