1 package cn.temptation;
2
3 public class Sample01 {
4 public static void main(String[] args) {
5 // 需求:小店对自己的销售额按季度进行统计,2016年各个季度统计如下:
6 // 第1季度:1月---10k 2月---12k 3月---9k
7 // 第2季度:4月---10k 5月---12k 6月---9k
8 // 第3季度:7月---10k 8月---12k 9月---9k
9 // 第4季度:10月---10k 11月---12k 12月---9k
10
11 // 考虑使用合适的数据类型来存储数据,因为每个季度中的每个月都记录下来的是其销售额,所以每个月记录的都是相同意义的数据
12 // 考虑使用数组存储
13 // 第1季度:int[] sale1 = { 10, 12, 9 };
14 // 第2季度:int[] sale2 = { 10, 12, 9 };
15 // 第3季度:int[] sale3 = { 10, 12, 9 };
16 // 第4季度:int[] sale4 = { 10, 12, 9 };
17
18 // 数组是一个容器,既然是容器,那么容器中存放元素的空间(即在堆内存开辟出来的空间)是否可以存储使用者指定类型的元素?
19 // 前面看到在空间中存储基本数据类型的数据是可以的,那么是否可以存储引用数据类型的数组呢? 答:可以的
20 // 类比:家里的冰箱是一个容器,冰箱里可以存放若干个小的容器冰格(大容器里可以放小容器)
21
22 // 二维数组的概念:元素为一维数组的(一维)数组
23
24 // 二维数组定义及初始化的形式1(动态初始化)
25 // 数据类型[][] 数组名 = new 数据类型[m][n];
26 // m:表示该二维数组有多少个一维数组
27 // n:表示每一个一维数组有多少个元素
28
29 int[][] yearSale = new int[4][3];
30 // 显示出的 [[ 表示是二维数组, 类比 显示出 [ 表示是一维数组
31 System.out.println(yearSale); // [[I@15db9742
32
33 System.out.println("------------------------------");
34
35 // yearSale[0]表示取得yearSale这个二维数组的第一个元素,也就是一个一维数组
36 System.out.println(yearSale[0]); // [I@6d06d69c
37 System.out.println(yearSale[1]); // [I@7852e922
38 System.out.println(yearSale[2]); // [I@4e25154f
39 System.out.println(yearSale[3]); // [I@70dea4e
40
41 System.out.println("------------------------------");
42
43 // 以yearSale[0]作为一个整体来看,yearSale[0][0]表示这个整体(一个一维数组)的第1个元素
44 System.out.println(yearSale[0][0]); // 0
45 System.out.println(yearSale[0][1]); // 0
46 System.out.println(yearSale[0][2]); // 0
47 }
48 }
1 package cn.temptation;
2
3 public class Sample02 {
4 public static void main(String[] args) {
5 // 一维数组定义有两种形式
6 // int[] arr1 = new int[3]; // 要求使用这种写法
7 // int arr2[] = new int[3];
8
9 // 二维数组定义形式1(要求使用这种写法)
10 int[][] yearSale1 = new int[4][3];
11 // 二维数组定义形式2(语法可以,但是不要这样写)
12 int yearSale2[][] = new int[4][3];
13 // 二维数组定义形式3(等你面试别人的时候问别人)
14 int[] yearSale3[] = new int[4][3];
15
16 System.out.println(yearSale1); // [[I@15db9742
17 System.out.println(yearSale2); // [[I@6d06d69c
18 System.out.println(yearSale3); // [[I@7852e922
19
20 // 问题:请说明下面变量的类型
21 int[] i, j[];
22 // 还是那句话"把没有见过的问题向见过的问题进行转换"
23 // 类比 int i, j; 等价于 int i; int j;
24 // 所以上面的语句按此推理应该等价于 int[] i; int[] j[];
25
26 // 推理之后还是应该验证一下
27 // // The local variable i may not have been initialized
28 // System.out.println(i); // 使用eclipse自动判定一下i的类型,为一维数组
29 // // The local variable j may not have been initialized
30 // System.out.println( j); // 使用eclipse自动判定一下j的类型,为二维数组
31 }
32 }
1 package cn.temptation;
2
3 public class Sample03 {
4 public static void main(String[] args) {
5 // 需求:小店对自己的销售额按季度进行统计,2016年各个季度统计如下:(3月初开店,11月底倒闭)
6 // 第1季度:3月---9k
7 // 第2季度:4月---10k 5月---12k 6月---9k
8 // 第3季度:7月---10k 8月---12k 9月---9k
9 // 第4季度:10月---10k 11月---12k
10
11 // 单纯从存储的角度看,下面这个二维数组是可以存储的(语法上OK)
12 // 但是会在元素数据的语义上有歧义,到底是这个月没有开张或是倒闭,还是这个月的销售额就是0
13 // int[][] yearSale = new int[4][3];
14 // 用上述语句进行数据存储时,不但需要知道二维数组中有多少个一级元素,而且还需要知道一级元素的最大长度是多少,否则会发生存储不下的问题
15
16 // 二维数组定义及初始化的形式2(动态初始化)
17 // 数据类型[][] 数组名 = new 数据类型[m][];
18 // m:表示该二维数组有多少个一维数组
19
20 int[][] yearSale = new int[4][];
21 // 上述语句说明了三件事:
22 // 1、这个变量的类型是一个二维int数组
23 // 2、这个二维数组的元素是一维数组类型
24 // 3、该二维数组的一级元素有4个一维数组组成
25 System.out.println(yearSale); // [[I@15db9742
26
27 System.out.println(yearSale[0]); // null
28 System.out.println(yearSale[1]); // null
29 System.out.println(yearSale[2]); // null
30 System.out.println(yearSale[3]); // null
31 // 引用数据类型的默认值为 null
32 // 上述语句中new int[4][]这句话,说明了在内存的堆中开辟空间做存储,只说明开辟出4个长度的空间,至于空间里放东西(存放数据)这个事情还没做
33 // 只知道向空间里放的东西(存放的数据)的类型是int类型的一维数组
34
35 // 使用二维数组定义及初始化的形式2(动态初始化)这种形式需要在二维数组定义及初始化后,还需要对二维数组的一级元素进行初始化
36 yearSale[0] = new int[1];
37 // 为什么非要放一个一维数组?可以放一个基本数据类型的数据不? 答:不行
38 // Type mismatch: cannot convert from int to int[]
39 // yearSale[0] = 123;
40 // 侧面证明了 Java语言 是 强类型语言(使用时的随意性得到约束),语法上严格的类型要求可以避免使用上的错误产生
41 yearSale[1] = new int[3];
42 yearSale[2] = new int[3];
43 yearSale[3] = new int[2];
44
45 System.out.println(yearSale[0]); // [I@6d06d69c
46 System.out.println(yearSale[1]); // [I@7852e922
47 System.out.println(yearSale[2]); // [I@4e25154f
48 System.out.println(yearSale[3]); // [I@70dea4e
49
50 System.out.println(yearSale[3][0]); // 0
51 System.out.println(yearSale[3][1]); // 0
52 // 产生异常:java.lang.ArrayIndexOutOfBoundsException: 1
53 // System.out.println(yearSale[0][1]);
54
55 // 如下写法可以不可以?
56 // 语法错误:Cannot specify an array dimension after an empty dimension 不能在一个空的维数后定义一个数组维数
57 // 这个语法错误可以理解为:第一个中括号中的数字表示的是这个二维数组有多少个一维数组
58 // 第一个中括号中没有数字也就是不知道有多少个一维数组(一级元素),在不知道的情况下就要去给每个一级元素开辟3个长度的空间,如何开辟?编译器做不到
59 // int[][] arr = new int[][3];
60 }
61 }
1 package cn.temptation;
2
3 public class Sample04 {
4 public static void main(String[] args) {
5 // 二维数组定义及初始化的形式3(静态初始化)
6
7 // 简写形式
8 // 数据类型[][] 数组名 = {
9 // { 二维数组的元素1的元素1, 二维数组的元素1的元素2, ..., 二维数组的元素1的元素n },
10 // { 二维数组的元素2的元素1, 二维数组的元素2的元素2, ..., 二维数组的元素2的元素n },...
11 // { 二维数组的元素m的元素1, 二维数组的元素m的元素2, ..., 二维数组的元素m的元素n },
12 // };
13 // m:表示该二维数组有多少个一维数组
14 // n:表示每个一维数组有多少个元素
15 // 注意:作为这种形式的二维数组的元素(一维数组)的长度可以一致,也可以不一致
16
17 int[][] yearSale = { { 10, 12, 9 }, { 10, 12, 9 }, { 10, 12, 9 }, { 10, 12, 9 }};
18 System.out.println(yearSale); // [[I@15db9742
19
20 int[][] scoreArr = { { 50 }, { 10, 20, 30 }, { 60, 70 } };
21 System.out.println(scoreArr); // [[I@6d06d69c
22
23 // 完整形式
24 // 数据类型[][] 数组名 = new 数据类型[][] {
25 // { 二维数组的元素1的元素1, 二维数组的元素1的元素2, ..., 二维数组的元素1的元素n },
26 // { 二维数组的元素2的元素1, 二维数组的元素2的元素2, ..., 二维数组的元素2的元素n },...
27 // { 二维数组的元素m的元素1, 二维数组的元素m的元素2, ..., 二维数组的元素m的元素n },
28 // };
29 // m:表示该二维数组有多少个一维数组
30 // n:表示每个一维数组有多少个元素
31 // 注意:作为这种形式的二维数组的元素(一维数组)的长度可以一致,也可以不一致
32
33 int[][] yearSaleEx = new int[][] { { 10, 12, 9 }, { 10, 12, 9 }, { 10, 12, 9 }, { 10, 12, 9 }};
34 System.out.println(yearSaleEx); // [[I@7852e922
35
36 int[][] scoreArrEx = { { 50 }, { 10, 20, 30 }, { 60, 70 } };
37 System.out.println(scoreArrEx); // [[I@4e25154f
38
39 System.out.println(scoreArr[0]); // [I@70dea4e
40 System.out.println(scoreArr[1]); // [I@5c647e05
41 System.out.println(scoreArr[2]); // [I@33909752
42
43 System.out.println(scoreArr[0][0]); // 50
44 }
45 }
1 package cn.temptation;
2
3 public class Sample05 {
4 public static void main(String[] args) {
5 // 二维数组的遍历
6 int[][] yearSale = { { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 }, { 77, 88, 99 } };
7
8 // 类比一维数组的遍历
9 // 这里二维数组的length属性表示二维数组包含的元素(一维数组)的个数
10 System.out.println(yearSale.length); // 4
11
12 // 最简单的思路
13 // for (int i = 0; i < yearSale[0].length; i++) {
14 // System.out.println(yearSale[0][i]);
15 // }
16 // for (int i = 0; i < yearSale[1].length; i++) {
17 // System.out.println(yearSale[1][i]);
18 // }
19 // for (int i = 0; i < yearSale[2].length; i++) {
20 // System.out.println(yearSale[2][i]);
21 // }
22 // for (int i = 0; i < yearSale[3].length; i++) {
23 // System.out.println(yearSale[3][i]);
24 // }
25
26 // 因为又发现重复的执行,考虑使用循环
27 // for (int i = 0; i < yearSale.length; i++) {
28 // for (int j = 0; j < yearSale[i].length; j++) {
29 // System.out.println(yearSale[i][j]);
30 // }
31 // }
32
33 // 调用方法
34 printArray(yearSale);
35 }
36
37 // 需求:制作一个二维数组的打印方法printArray,显示例如:[[10, 20, 30], [40, 50, 60], [70, 80, 90], [77, 88, 99]]
38 // 写法1、在一个方法中打印显示
39 /**
40 * 二维数组打印方法
41 * @param yearSale:二维数组
42 */
43 // public static void printArray(int[][] arr) {
44 // System.out.print("[");
45 //
46 // // 遍历二维数组
47 // for (int i = 0; i < arr.length; i++) {
48 // if (i == arr.length - 1) { // 二维数组的最后一个元素 加上"]"
49 // // 遍历一维数组
50 // System.out.print("[");
51 //
52 // for (int j = 0; j < arr[i].length; j++) {
53 // if (j == arr[i].length - 1) { // 一维数组的最后一个元素 加上"]"
54 // System.out.print(arr[i][j] + "]");
55 // } else { // 一维数组的其他元素 加上","
56 // System.out.print(arr[i][j] + ",");
57 // }
58 // }
59 //
60 // // 二维数组的最后一个元素 加上"]"
61 // System.out.print("]");
62 // } else { // 二维数组的其他元素 加上","
63 // // 遍历一维数组
64 // System.out.print("[");
65 //
66 // for (int j = 0; j < arr[i].length; j++) {
67 // if (j == arr[i].length - 1) { // 一维数组的最后一个元素 加上"]"
68 // System.out.print(arr[i][j] + "]");
69 // } else { // 一维数组的其他元素 加上","
70 // System.out.print(arr[i][j] + ",");
71 // }
72 // }
73 //
74 // // 二维数组的其他元素 加上","
75 // System.out.print(",");
76 // }
77 // }
78 //
79 // // 换行
80 // System.out.println();
81 // }
82
83 // 写法2、不同的事情在不同的方法中做
84 /**
85 * 二维数组的打印方法
86 * @param arr:二维数组
87 */
88 public static void printArray(int[][] arr) {
89 System.out.print("[");
90
91 // 遍历二维数组
92 for (int i = 0; i < arr.length; i++) {
93 // 1、调用一维数组的打印方法
94 printArray(arr[i]);
95
96 // 2、打印完一维数组后,添加不同的后续内容
97 if (i == arr.length - 1) { // 二维数组的最后一个元素 加上"]"
98 System.out.print("]");
99 } else { // 二维数组的最后一个元素 加上","
100 System.out.print(",");
101 }
102 }
103
104 // 换行
105 System.out.println();
106 }
107
108 /**
109 * 一维数组的打印方法
110 * @param arr:一维数组
111 */
112 public static void printArray(int[] arr) {
113 System.out.print("[");
114
115 // 遍历数组
116 for (int i = 0; i < arr.length; i++) {
117 if (i == arr.length - 1) { // 一维数组的最后一个元素 加上"]"
118 System.out.print(arr[i] + "]");
119 } else { // 一维数组的最后一个元素 加上","
120 System.out.print(arr[i] + ",");
121 }
122 }
123 }
124 }
1 package cn.temptation;
2
3 public class Sample06 {
4 public static void main(String[] args) {
5 // 需求:小店对自己的销售额按季度进行统计,2016年各个季度统计如下:(3月初开店,11月底倒闭)
6 // 第1季度:3月---4k
7 // 第2季度:4月---10k 5月---12k 6月---9k
8 // 第3季度:7月---10k 8月---19k 9月---9k
9 // 第4季度:10月---10k 11月---12k
10 // 使用二维数组计算2016年该店的销售总和、月最高销售额、月最低销售额
11
12 // int[][] saleArr = { { 4 }, { 10, 12, 9 }, { 10, 19, 9 }, { 10, 12 } };
13
14 // 下句语法正确,再次理解"二维数组就是元素为一维数组的一维数组"
15 int[][] saleArr = {};
16 // System.out.println(saleArr); // [[I@15db9742
17 // System.out.println(saleArr.length); // 0
18
19 System.out.println("2016年该店的销售总和为:" + yearTotal(saleArr) + "k元");
20
21 System.out.println("2016年该店的月最高销售额为:" + monthMaxSale(saleArr) + "k元");
22
23 System.out.println("2016年该店的月最低销售额为:" + monthMinSale(saleArr) + "k元");
24 }
25
26 /**
27 * 计算年销售总和
28 * @param arr:二维数组
29 * @return:年销售总和
30 */
31 public static int yearTotal(int[][] arr) {
32 int total = 0;
33
34 for (int i = 0; i < arr.length; i++) {
35 for (int j = 0; j < arr[i].length; j++) {
36 total += arr[i][j];
37 }
38 }
39
40 return total;
41 }
42
43 /**
44 * 统计月最高销售额
45 * @param arr:二维数组
46 * @return:月最高销售额
47 */
48 public static int monthMaxSale(int[][] arr) {
49 int max = 0;
50
51 for (int i = 0; i < arr.length; i++) {
52 for (int j = 0; j < arr[i].length; j++) {
53 max = (max > arr[i][j]) ? max : arr[i][j];
54 }
55 }
56
57 return max;
58 }
59
60 /**
61 * 统计月最低销售额
62 * @param arr:二维数组
63 * @return:月最低销售额
64 */
65 public static int monthMinSale(int[][] arr) {
66 // 如下写法逻辑错误,如果没有比0小的销售额,最小值就一直为0
67 // int min = 0;
68
69 // 如下写法对于没有销售额的二维数组来说,执行时会产生异常
70 // 产生异常:java.lang.ArrayIndexOutOfBoundsException: 0
71 // 考虑设置二维数组的第一个二级元素为比较的标杆
72 // int min = arr[0][0];
73
74 // 如果没有销售额时,如何处理?
75 int min = (arr.length == 0) ? 0 : arr[0][0];
76
77 for (int i = 0; i < arr.length; i++) {
78 for (int j = 0; j < arr[i].length; j++) {
79 min = (min < arr[i][j]) ? min : arr[i][j];
80 }
81 }
82
83 return min;
84 }
85 }
1 package cn.temptation;
2
3 import java.util.Scanner;
4
5 public class Sample07 {
6 public static void main(String[] args) {
7 // 需求:编写程序,输出如下图形(杨辉三角)
8 // 1
9 // 1 1
10 // 1 2 1
11 // 1 3 3 1
12 // 1 4 6 4 1
13 // 1 5 10 10 5 1
14
15 // 思路:(找规律)
16 // 1、第n行有n个数(n从1开始)
17 // 2、第1行和第2行均为1,从第3行开始,每行的首尾均为1
18 // 3、从第3行的第2列开始到该行的倒数第二个数结束,该位置上的数等于上一行同列的数 + 上一行前一列的数
19 // 从第4行的第2列开始到该行的倒数第二个数结束,该位置上的数等于上一行同列的数 + 上一行前一列的数
20 // ......以此类推
21
22 System.out.println("键盘录入杨辉三角的行数:");
23 Scanner input = new Scanner(System.in);
24 int row = input.nextInt();
25 input.close();
26
27 // 考虑存储杨辉三角中的数进行计算,都是数字(具有相同意义),考虑使用数组;
28 // 因为涉及到行列,考虑使用二维数组;
29 // 因为行中的列数不同,所以声明及初始化数组时使用只有一级元素长度没有二级元素长度的定义方式
30 int[][] arr = new int[row][];
31
32 // 根据规律1
33 // arr[0] = new int[1];
34 // arr[1] = new int[2];
35 // arr[2] = new int[3];
36 // ...
37 // arr[n] = new int[n+1];
38
39 for (int i = 0; i < row; i++) {
40 // 二维数组中的每个一级元素依据规律1创建相应长度的一维数组
41 arr[i] = new int[i + 1];
42
43 // 依据规律2:第1行和第2行均为1,从第3行开始,每行的首尾均为1
44 arr[i][0] = 1;
45 arr[i][i] = 1;
46 }
47
48 // 依据规律3:从第3行的第2列开始到该行的倒数第二个数结束,该位置上的数等于上一行同列的数 + 上一行前一列的数
49 // 从第4行的第2列开始到该行的倒数第二个数结束,该位置上的数等于上一行同列的数 + 上一行前一列的数
50 // ......以此类推
51 for (int i = 2; i < arr.length; i++) { // 外层循环的i表示行
52 for (int j = 1; j < arr[i].length - 1; j++) { // 内层循环的j表示列
53 arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
54 }
55 }
56
57 // 调用方法打印数组
58 printArray(arr);
59 }
60
61 /**
62 * 打印数组
63 * @param arr:二维数组
64 */
65 public static void printArray(int[][] arr) {
66 // 遍历数组
67 for (int i = 0; i < arr.length; i++) {
68 for (int j = 0; j < arr[i].length; j++) {
69 System.out.print(arr[i][j] + "\t");
70 }
71
72 // 换行
73 System.out.println();
74 }
75 }
76 }
1 package cn.temptation;
2
3 import java.util.Scanner;
4
5 public class Sample08 {
6 public static void main(String[] args) {
7 // 需求:制作一个成绩查询系统
8 // 提供的数据有:
9 // 1、给定学生的学号(1001, 1002, 1003)
10 // 2、给定学生的三门功课的成绩(语文、数学、英语)
11 // 要求:
12 // 1、输入某个同学的学号,显示出该同学的三门功课的成绩
13 // 2、输入某一门功课的编号(0---语文,1---数学,2---英语),显示该门功课成绩不及格的同学的学号,以及有多少个同学不及格
14
15 // 关键点:学生的学号 和 其各门功课成绩如何对应起来? 考虑让学号数组的索引 和 成绩数组的一级元素的索引同步
16 int[] studentArr = { 1001, 1002, 1003 };
17 int[][] scoreArr = { { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 } };
18 // 课程数组的索引 和 成绩数组的二级元素的索引同步
19 String[] courseArr = { "语文", "数学", "英语" };
20
21 // 如下写法把学号和成绩糅合在一起存储也可以,但是不建议这样存储,因为我们说过数组是存储同一类型数据的容器
22 // 这里说的同一类型不仅指的是同一种数据类型,也指的是有相同的语义类型的数据
23 // int[][] arr = { { 1001, 10, 20, 30 }, { 1002, 40, 50, 60 }, { 1003, 70, 80, 90 } };
24
25 Scanner input = new Scanner(System.in);
26 System.out.println("输入学生的学号:");
27 int number = input.nextInt();
28 // 调用方法
29 getScoreByNumber(courseArr, studentArr, scoreArr, number);
30
31 // 换行
32 System.out.println();
33
34 System.out.println("输入某一门功课的编号(0---语文,1---数学,2---英语)");
35 int course = input.nextInt();
36 // 调用方法
37 getNGInfo(courseArr, studentArr, scoreArr, course);
38
39 input.close();
40 }
41
42 /**
43 * 根据学号获取该生的各门功课成绩
44 * @param courseArr: 课程名称数组
45 * @param studentArr: 学生学号数组
46 * @param scoreArr: 学生成绩数组
47 * @param number: 查询时录入的学生学号
48 */
49 public static void getScoreByNumber(String[] courseArr, int[] studentArr, int[][] scoreArr, int number) {
50 for (int i = 0; i < studentArr.length; i++) {
51 if (number == studentArr[i]) { // 根据录入的学生学号在数组中找到对应的该学生的学号
52 // 因为学号数组的索引 和 成绩数组的一级元素的索引同步,所以在 成绩数组的一级元素对应的一维数组中查找该生的各门功课的成绩
53 for (int j = 0; j < scoreArr[i].length; j++) {
54 if (j == scoreArr[i].length - 1) {
55 System.out.print(courseArr[j] + ":" + scoreArr[i][j]);
56 } else {
57 System.out.print(courseArr[j] + ":" + scoreArr[i][j] + ",");
58 }
59 }
60 }
61 }
62 }
63
64 /**
65 * 显示该门功课成绩不及格的同学的学号,以及有多少个同学不及格
66 * @param courseArr: 课程名称数组
67 * @param studentArr: 学生学号数组
68 * @param scoreArr: 学生成绩数组
69 * @param course: 录入的课程名称索引
70 */
71 public static void getNGInfo(String[] courseArr, int[] studentArr, int[][] scoreArr, int course) {
72 int total = 0;
73
74 System.out.println("【" + courseArr[course] + "】未及格学生的学号列表:");
75 for (int i = 0; i < studentArr.length; i++) {
76 if (scoreArr[i][course] < 60) {
77 total++;
78 System.out.print(studentArr[i] + "\t");
79 }
80 }
81
82 // 换行
83 System.out.println();
84
85 System.out.println("【" + courseArr[course] + "】未及格学生的人数为:" + total);
86 }
87 }