# 数组

  • 程序 = 算法 + 数据结构
  • 算法,像if...esle... , switch...case.. ,while , do-while, for等解决流程问题都是算法
  • 数据结构,就是将数据按照特定结构保存在内存中的一种结构

数组在 java 中就是将相同类型的数据(元素)集合在一起

/**
 * 3个学生成绩
 */
//
int s1 = 100;
int s2 = 90;
int s3 = 80;

// 数组格式写法 两种
// 第一种
int[] scores = new int[3];
// 第二种
//        int scores[] = new int[3];
// 给数组赋值
scores[0] = 100;
scores[1] = 90;
scores[2] = 80;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

语法:

数组类型[] 数组名 = new 数据类型[大小];
// 或者
数组类型 数组名[] = new 数据类型[大小];
// 大小是数组的长度,也就是数组中元素的个数
1
2
3
4

定义数组,例如:

byte[] arr1 = new byte[5]
short[] arr2 = new short[5]
int[] arr3 = new int[5];
long[] arr4 = new long[5];
float[] arr5 = new float[5];
double[] arr6 = new double[5];
char[] arr7 = new char[5];
boolean[] arr8 = new boolean[5];
String[] arr9 = new String[5];
// ...

1
2
3
4
5
6
7
8
9
10
11

或者

byte arr1[] = new byte[5]
short arr2[] = new short[5]
int arr3[] = new int[5];
long arr4[] = new long[5];
float arr5[] = new float[5];
double arr6[] = new double[5];
char arr7[] = new char[5];
boolean arr8[] = new boolean[5];
String arr9[] = new String[5];
1
2
3
4
5
6
7
8
9

备注:

  • 数组类型和数据类型必须一致
  • 有数组名
  • [] 不可缺少

# 数组的初始化

基本类型创建数组后,其元素的初始值: byte、short、int、long、char、是0 float、double是0.0 boolean是false
引用类型 String 初始值是 null

/**
* 数组初始化
*/
// 1. 声明时同时初始化数组
int[] scores2 = {100, 90, 80};
// 2. 先声明,后初始化数组,长度[]里值可以不写,元素个数就是长度
int[] scores3;
scores3 = new int[]{100, 90, 80};
1
2
3
4
5
6
7
8

# 数组的访问

# 获取数组长度

语法:
数组名.length

// 1. 数组的长度
int[] scores = {100, 90, 80};
System.out.println(scores.length); // 3
1
2
3

备注: 数组长度也就是数组中元素的个数

# 获取数组元素

语法:
数组名[下标]

// 1. 数组的长度
int[] scores = {100, 90, 80};
System.out.println(scores[0]); // 100
System.out.println(scores[1]); // 90
System.out.println(scores[2]); // 80
// 将下标为 1 的元素值,赋值给下边为2 的元素
scores[2] = scores[1]; 
System.out.println(scores[2]); // 90
// 2. 数组的元素访问,超出范围报错
// System.out.println(scores[3]);
// 3. 数组的元素访问,超出范围,可以用下面的方式访问
System.out.println(scores[scores.length - 1]);
1
2
3
4
5
6
7
8
9
10
11
12

备注:
下标是从0开始,最大是 length - 1

# 数组的遍历

// 1. 遍历数组
// 遍历数组给每个元素赋值
int[] scores2 = new int[10];
for (int i = 0; i < scores2.length; i++) {
    scores2[i] = 100;
}
// 遍历数组,输出每个元素的值
int[] scores3 = {100, 90, 80};
for (int i = 0; i < scores3.length; i++) {
    System.out.println(scores3[i]);
}
/**
 * for each循环遍历数组
 * 中间 冒号隔开 “:”
 * 注意:for each循环遍历数组,下面的变量 n 是 元素,而不是下标
 */
for (int n:scores3) {
    System.out.println(n);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 数组的拷贝

  • System.arraycopy(src, srcPos, dest, destPos, length)) java自带的方法,拷贝数组
    • src: 源数组
    • srcPos: 源数组的起始位置
    • dest: 目标数组
    • length: 拷贝的长度
  • Arrays.copyOf(original, newLength) JDK提供的方法,需要引入包 java.util.Arrays, 用于复制数组,返回一个新数组
    • original: 源数组
    • newLength: 新数组的长度
 /**
 * 复制数组
 *
 *  System.arraycopy(src, srcPos, dest, destPos, length));
 *  src:源数组
 *  srcPos:源数组的开始位置
 *  dest:目标数组
 *  destPos:目标数组的开始位置
 *  length:拷贝的长度
 */
// scores 有5个元素
int[] scores = {100, 90, 80, 90, 80};
System.out.println("scores.length="+scores.length);
int[] scores1 = new int[6];
System.out.println("scores1.length="+scores1.length);

System.arraycopy(scores, 1, scores1, 0, 4);
for (int i = 0; i < scores1.length; i++) {
    System.out.println(scores1[i]);
}

/**
 * 复制数组
 *
 * Arrays.copyOf(original, newLength)
 * original:源数组
 * newLength:新数组的长度
 */
int[] scores2 = java.util.Arrays.copyOf(scores, 3);
for (int i = 0; i < scores2.length; i++) {
    System.out.println(scores2[i]);
}
/**
 * 数组扩容
 * 数组长度在创建后是不可变的,可以通过Arrays.copyOf(original, newLength)方法中修改newLength参数值来扩容数组长度
 * 例如下面:扩容了3个元素,原数组长度为5,新数组长度为8
 */
scores2 = java.util.Arrays.copyOf(scores, scores.length + 3);
for (int i = 0; i < scores2.length; i++) {
    System.out.println(scores2[i]);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

备注:

  • Arrays.copyOf 是JDK提供的方法,需要引入包 java.util.Arrays
  • Arrays.copyOf 方法可以修改newLength参数值来扩容数组长度

# 数组的排序

  • 使用 for 循环 冒泡排序
  • 使用 JDK 提供的 Arrays.sort() 方法排序
  • ...
 /**
   * 数组排序,从小到大排序
   * 9组数据
   */
  int[] scores = {100, 90, 80, 75, 95, 82, 84, 91, 76};
  /*
      冒泡排序
      两两比较,如果前一个比后一个大,就交换位置
   */
  // 冒泡排序
//        for (int i = 0; i < scores.length; i++) {
//            for (int j = 0; j < scores.length - 1; j++) {
//                if (scores[j] > scores[j + 1]) {
//                    int temp = scores[j];
//                    scores[j] = scores[j + 1];
//                    scores[j + 1] = temp;
//                }
//            }
//        }
  // 与上面一样
//        for (int i = 0; i < scores.length; i++) {
//            for (int j = 1; j < scores.length; j++) {
//                if (scores[j-1] > scores[j]) {
//                    int temp = scores[j-1];
//                    scores[j-1] = scores[j];
//                    scores[j] = temp;
//                }
//            }
//        }
  /*
      使用JDK提供 Arrays.sort(数组) 方法排序
      参数:数组
   */
  Arrays.sort(scores);
  //  JDK提供的java.util工具类中Arrays.toString()方法,可以将数组转换成字符串
  System.out.println(Arrays.toString(scores));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

备注:

  • Arrays.sort 是JDK提供的方法,需要引入包 java.util.Arrays
  • Arrays.sort 方法参数是数组
  • Arrays.toString() 方法,是JDK提供的方法,可以将数组转换成字符串

# 多维数组

多维数组,就是数组里面还有数组,也叫嵌套数组

/**
 * 多维数组,就是数组里面还有数组,或者是嵌套数组
 */
// 普通数组,也叫一维数组
int[] scores = {100, 90, 80};
System.out.println(Arrays.toString(scores));
// 二维数组
int[][] scores2 = {
        {100, 90, 80},
        {90, 80, 70}
};
System.out.println(Arrays.toString(scores2));
// 多维数组打印使用 JDK提供的 Arrays.deepToString(数组) 方法
System.out.println(Arrays.deepToString(scores2));
// 三维数组
int[][][] scores3 = {
        {
                {100, 90, 80},
                {90, 80, 70}
        },
        {
                {100, 90, 80},
                {90, 80, 70}
        }
};
System.out.println(Arrays.toString(scores3));
System.out.println(Arrays.deepToString(scores3));
// ... 更多维数组

// for循环多维数组
for (int i = 0; i < scores3.length; i++) {
    for (int j = 0; j < scores3[i].length; j++) {
        for (int k = 0; k < scores3[i][j].length; k++) {
            System.out.print(scores3[i][j][k] + " ");
        }
        System.out.println();
    }
    System.out.println();
}
System.out.println("-----------------------------------------------------");
// for-each循环多维数组
for (int[][] scores3_i : scores3) {
    for (int scores3_i_j[] : scores3_i) {
        for ( int scores3_i_j_k : scores3_i_j) {
            System.out.print(scores3_i_j_k + " ");
        }
    }
    System.out.println();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

备注:

  • Arrays.deepToString 方法,是JDK提供的方法,可以将多维数组转换成字符串