5.3、数组常见操作【重点】【难点】【面试点】
5.3.1、动态赋值【重点】
int[] scores=new int[5]; Scanner scanner=new Scanner(System.in); for (int i = 0; i < scores.length; i++) { System.out.println("请输入第"+(i+1)+"个学生的成绩:"); scores[i]=scanner.nextInt(); } |
5.3.2、循环取值【重点】
System.out.println("\n-----------学生成绩列表------------\n"); for (int i = 0; i < scores.length; i++) { System.out.println("第"+(i+1)+"个学生的成绩:"+scores[i]); sum+=scores[i]; } |
5.3.3、求极值
public class Demo3 { public static void main(String[] args) { int[] numbers = { 12, 56, 78, 23, 100, 24, 4556, 1, 27, 89 }; int max = numbers[0]; int maxIndex = 0; int min = numbers[0]; int minIndex = 0; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; maxIndex = i; } if (numbers[i] < min) { min = numbers[i]; minIndex = i; } } System.out.println("max:" + max); System.out.println("maxIndex:" + maxIndex); System.out.println("min:" + min); System.out.println("minIndex:" + minIndex); } } |
5.3.4、排序【重点】【难点】【面试点】
int[] nums=new int[] {12,3434,1,234,23,46,34,1111,77,13}; for (int i = 0; i < nums.length-1; i++) { for (int j = 0; j < nums.length-i-1; j++) { if(nums[j]<nums[j+1]) { int temp=nums[j]; nums[j]=nums[j+1]; nums[j+1]=temp; } } } |
5.3.5、查找【难点】
public class Demo5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入您要查找的数据:"); int find = scanner.nextInt(); int[] numbers = { 1, 232, 34, 5634, 3423, 756, 224, 8, 523, 23, 6, 34, 61 }; int i = 0; for (; i < numbers.length; i++) { if (find == numbers[i]) break; } if (i == numbers.length) System.out.println("抱歉,您要查找的数据不存在!"); else { System.out.println("请输入您要替换的数据:"); int replace = scanner.nextInt(); numbers[i] = replace; for (int j = 0; j < numbers.length; j++) { System.out.print(numbers[j]+" "); } } } } |
5.3.6、插入【难点】
public class Demo6 { public static void main(String[] args) { int[] numbers = new int[6]; numbers[0] = 12; numbers[1] = 22; numbers[2] = 52; numbers[3] = 112; numbers[4] = 1212; numbers[5] = 0; Scanner scanner = new Scanner(System.in); System.out.println("请输入您要插入的数据:"); int insert = scanner.nextInt(); int i = 0; for (; i < numbers.length - 1; i++) { if (insert < numbers[i]) break; } for (int j = numbers.length - 1; i < j; j--) { numbers[j] = numbers[j - 1]; } numbers[i] = insert; for (int j = 0; j < numbers.length; j++) { System.out.print(numbers[j] + " "); } } } |
5.3.7、删除【难点】
public class Demo7 { public static void main(String[] args) { int[] numbers = { 1, 2, 4, 5, 32, 234, 36, 23, 56, 7, 34 }; Scanner scanner = new Scanner(System.in); System.out.println("请输入您要删除的数据:"); int remove = scanner.nextInt(); int i = 0; for (; i < numbers.length; i++) { if (remove == numbers[i]) break; } if(i!=numbers.length) { for (; i < numbers.length - 1; i++) { numbers[i] = numbers[i + 1]; } for (int j = 0; j < numbers.length-1; j++) { System.out.print(numbers[j]+" "); } }else System.out.println("没有找到要删除的数据!"); } } |
5.4、小结
数组是使用连续内存单元存储的一组相同数据类型的数据;数组能够批量管理数据,降低程序的复杂度,提升开发效率。
数组的声明语法是:数据类型[] 数组名=new 数据类型[长度],声明后长度是不可更改的,使用时采用下标进行访问其中的元素,下标从0开始计数。【重点】
数组的常见操作有:遍历、求极值、排序、查找、插入、删除,其中排序追常见的算法是冒泡排序后法。【难点】