数组的概述

  • 多个相同类型数据按照一定的顺序排列的组合

常见概念

  • 数组名
  • 下标(索引)
  • 元素
  • 数组的长度

一维数组的使用

声明和初始化

  1. int[] a; // 声明
  2. a = new int[]{1,2,3,4}; // 静态初始化
  3. String[] s = new String[5] // 动态初始化

获取指定位置的元素

  • 通过索引下标 (从0开始到数组长度-1结束)

    1. a[0] // 1
    2. a[1] // 2

    长度的获取

  • 属性:length

    1. a.length // 4

    遍历数组

    1. for(int i = 0 ; i < a.length ; i ++ ){
    2. System.out.println(a[i]);
    3. }

    数组的默认初始化值

  • 整型:0

  • 浮点型:0.0
  • char型:0 或者 ‘\u0000’
  • boolean型:false
  • 引用型:null

    数组的内存解析

  • 内存的简化结构

练习题1:

升景坊单间短期出租4个月,550元/月(水电煤公摊,网费35元/月),空调、卫生间、厨房齐全。
屋内均是IT行业人士,喜欢安静。所以要求来租者最好是同行或者刚毕业的年轻人,爱干净、安静。

求联系方式:

  1. public class ArrayDemo {
  2. public static void main(String[] args) {
  3. int[] arr = new int [] { 8,2,1,0,3 };
  4. int[] index = new int [] { 2,0,3,2,4,0,1,3,2,3,3};
  5. String tel = "";
  6. for(int i = 0 ; i < index.length ; i ++ ){
  7. tel += arr[index[i]];
  8. }
  9. System.out.println("联系方式:" + tel);
  10. }
  11. }
  12. 联系方式:18013820100

练习题2:
从键盘读入学生成绩,找出最高分,并输出学生成绩等级。 
成绩>=最高分-10 等级为’A’ 
成绩>=最高分-20 等级为’B’ 
成绩>=最高分-30 等级为’C’
其余 等级为’D’
假设依次输入:56 74 89 41 89

  1. package pers.zyx.exer;
  2. import java.util.Scanner;
  3. import static sun.swing.MenuItemLayoutHelper.max;
  4. public class ArrayDemo {
  5. public static void main(String[] args) {
  6. Scanner input = new Scanner(System.in);
  7. System.out.println("请输入学生的个数:");
  8. int number = input.nextInt();
  9. int[] arr = new int [number];
  10. int maxScore = -1;
  11. System.out.println("请输入学生的成绩");
  12. for(int i = 0 ; i < number ; i ++ ){
  13. arr[i] = input.nextInt();
  14. maxScore = max(maxScore , arr[i]);
  15. }
  16. System.out.println("最高分: " + maxScore);
  17. for(int i = 0 ; i < number ; i ++ ){
  18. System.out.print("Student" + i + " score is " + arr[i] + " grade is ");
  19. if (arr[i] >= maxScore - 10) {
  20. System.out.println("A");
  21. }
  22. else if (arr[i] >= maxScore - 20){
  23. System.out.println("B");
  24. }else if (arr[i] >= maxScore - 30){
  25. System.out.println("C");
  26. }else{
  27. System.out.println("D");
  28. }
  29. }
  30. }
  31. }
  32. 请输入学生的个数:
  33. 5
  34. 请输入学生的成绩
  35. 56 74 89 41 89
  36. 最高分: 89
  37. Student0 score is 56 grade is D
  38. Student1 score is 74 grade is B
  39. Student2 score is 89 grade is A
  40. Student3 score is 41 grade is D
  41. Student4 score is 89 grade is A

多维数组的使用

声明和初始化

  1. int[][] arr = new int[][]{{1,2,3,4},{4,5,6,7},{7,8,9,10}}; // 静态初始化
  2. String[][] s = new String[3][2] // 动态初始化

获取指定位置的元素

  1. arr[0][0] // 1
  2. arr[2][1] // 8

长度的获取

  1. arr.length // 4
  2. arr[1].length // 3

遍历数组

  1. for(int i = 0 ; i < arr.length ; i ++ ){
  2. for(int j = 0 ; j < arr[i].length ; j ++){
  3. System.out.println(arr[i][j]);
  4. }
  5. }

数组的默认初始化值

  1. int[][] arr = new int[3][2]
  2. arr[0] // 0行的地址
  3. arr[0][0] // 0

数组的内存解析

举例:
int[][] arr = new int[4][];
arr[1] = new int[]{1,2,3,4};
练习题1:

获取arr数组中所有元素的和。

  1. package pers.zyx.exer;
  2. public class ArrayDemo {
  3. public static void main(String[] args) {
  4. int[][] arr = new int[][]{{3,5,8},{12,9},{7,0,6,4}};
  5. int sum = 0;
  6. for(int i = 0 ; i < arr.length ; i ++ ){
  7. for(int j = 0 ; j < arr[i].length ; j ++ ){
  8. sum += arr[i][j];
  9. }
  10. }
  11. System.out.println(sum);
  12. }
  13. }
  14. 54

练习题2:

使用二维数组打印一个 10 行杨辉三角。
提示

  1. 第一行有 1 个元素, 第 n 行有 n 个元素
  2. 每一行的第一个元素和最后一个元素都是 1
  3. 从第三行开始, 对于非第一个元素和最后一个元
    素的元素。即:
    yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
  1. public class ArrayDemo {
  2. public static void main(String[] args) {
  3. int[][] arr = new int[10][];
  4. for(int i = 0 ; i < 10 ; i ++ ){
  5. arr[i] = new int[i + 1];
  6. arr[i][0] = 1;
  7. arr[i][i] = 1;
  8. if( i > 1){
  9. for(int j = 1 ; j < arr[i].length - 1 ; j ++ ){
  10. arr[i][j] = arr[i - 1][j -1 ] + arr[i - 1][j];
  11. }
  12. }
  13. }
  14. for(int i = 0 ; i < 10 ; i ++ ){
  15. for(int j = 0 ; j < arr[i].length ; j ++ ){
  16. System.out.print(arr[i][j] + " ");
  17. }
  18. System.out.println();
  19. }
  20. }
  21. }

排序算法

排序方法 时间复杂度(平均) 时间复杂度(最坏) 时间复杂度(最好) 空间复杂度 稳定性
插入排序 O(n^2) O(n^2) O(n) O(1) 稳定
希尔排序 O(n^1.3) O(n^2) O(n) O(1) 不稳定
选择排序 O(n^2) O(n^2) O(n^2) O(1) 不稳定
堆排序 O(nlog2n) O(nlog2n) O(nlog2n) O(1) 不稳定
冒泡排序 O(n^2) O(n^2) O(n) O(1) 稳定
快速排序 O(nlog2n) O(n^2) O(nlog2n) O(nlog2n) 不稳定
归并排序 O(nlog2n) O(nlog2n) O(nlog2n) O(n) 稳定
计数排序 O(n+k) O(n+k) O(n+k) O(n+k) 稳定
桶排序 O(n+k) O(n^2) O(n) O(n+k) 稳定
基数排序 O(n*k) O(n*k) O(n*k) O(n+k) 稳定

插入排序

(直接)插入排序.gif

希尔排序希尔排序.gif

选择排序

(简单)选择排序.gif

堆排序

堆排序.gif

冒泡排序

冒泡排序.gif快速排序

快速排序.gif

归并排序

归并排序.gif

计数排序计数排序.gif

桶排序

桶排序.png

基数排序

基数排序.gif

Arrays工具类的使用

  • java.util.Arrays 类即为操作数组的工具类,包含了用来操作数组的各种方法。

数组使用中的常见异常

  • 数组脚标越界异常(ArrayIndexOutOfBoundsException)
  • 空指针异常(NullPointerException)

    1. <br />