Skip to content

Commit dec88ef

Browse files
committed
Add ch06 code
1 parent eea17c6 commit dec88ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+932
-0
lines changed

code/chapter06/Array01.class

1.01 KB
Binary file not shown.

code/chapter06/Array01.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
//数组的引出
3+
//
4+
public class Array01 {
5+
6+
//编写一个main方法
7+
public static void main(String[] args) {
8+
/*
9+
它们的体重分别是3kg,5kg,1kg,3.4kg,2kg,50kg 。
10+
请问这六只鸡的总体重是多少?平均体重是多少?
11+
12+
思路分析
13+
1. 定义六个变量 double , 求和 得到总体重
14+
2. 平均体重 = 总体重 / 6
15+
3. 分析传统实现的方式问题. 6->600->566
16+
4. 引出新的技术 -> 使用数组来解决.
17+
*/
18+
19+
// double hen1 = 3;
20+
// double hen2 = 5;
21+
// double hen3 = 1;
22+
// double hen4 = 3.4;
23+
// double hen5 = 2;
24+
// double hen6 = 50;
25+
26+
// double totalWeight = hen1 + hen2 + hen3 + hen4 + hen5 + hen6;
27+
28+
// double avgWeight = totalWeight / 6;
29+
// System.out.println("总体重=" + totalWeight
30+
// + "平均体重=" + avgWeight);
31+
32+
//比如,我们可以用数组来解决上一个问题 => 体验
33+
//
34+
//定义一个数组
35+
//老韩解读
36+
//1. double[] 表示 是double类型的数组, 数组名 hens
37+
//2. {3, 5, 1, 3.4, 2, 50} 表示数组的值/元素,依次表示数组的
38+
// 第几个元素
39+
//
40+
double[] hens = {3, 5, 1, 3.4, 2, 50, 7.8, 88.8,1.1,5.6,100};
41+
42+
//遍历数组得到数组的所有元素的和, 使用for
43+
//老韩解读
44+
//1. 我们可以通过 hens[下标] 来访问数组的元素
45+
// 下标是从 0 开始编号的比如第一个元素就是 hens[0]
46+
// 第2个元素就是 hens[1] , 依次类推
47+
//2. 通过for就可以循环的访问 数组的元素/值
48+
//3. 使用一个变量 totalWeight 将各个元素累积
49+
System.out.println("===使用数组解决===");
50+
//老师提示: 可以通过 数组名.length 得到数组的大小/长度
51+
//System.out.println("数组的长度=" + hens.length);
52+
double totalWeight = 0;
53+
for( int i = 0; i < hens.length; i++) {
54+
//System.out.println("第" + (i+1) + "个元素的值=" + hens[i]);
55+
totalWeight += hens[i];
56+
}
57+
58+
System.out.println("总体重=" + totalWeight
59+
+ "平均体重=" + (totalWeight / hens.length) );
60+
61+
62+
63+
}
64+
}

code/chapter06/Array02.class

1.06 KB
Binary file not shown.

code/chapter06/Array02.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.Scanner;
2+
public class Array02 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
//演示 数据类型 数组名[]=new 数据类型[大小]
7+
//循环输入5个成绩,保存到double数组,并输出
8+
9+
//步骤
10+
//1. 创建一个 double 数组,大小 5
11+
12+
//(1) 第一种动态分配方式
13+
//double scores[] = new double[5];
14+
//(2) 第2种动态分配方式, 先声明数组,再 new 分配空间
15+
double scores[] ; //声明数组, 这时 scores 是 null
16+
scores = new double[5]; // 分配内存空间,可以存放数据
17+
18+
19+
//2. 循环输入
20+
// scores.length 表示数组的大小/长度
21+
//
22+
Scanner myScanner = new Scanner(System.in);
23+
for( int i = 0; i < scores.length; i++) {
24+
System.out.println("请输入第"+ (i+1) +"个元素的值");
25+
scores[i] = myScanner.nextDouble();
26+
}
27+
28+
//输出,遍历数组
29+
System.out.println("==数组的元素/值的情况如下:===");
30+
for( int i = 0; i < scores.length; i++) {
31+
System.out.println("第"+ (i+1) +"个元素的值=" + scores[i]);
32+
}
33+
}
34+
}

code/chapter06/ArrayAdd.class

838 Bytes
Binary file not shown.

code/chapter06/ArrayAdd.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
public class ArrayAdd {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
/*
7+
要求:实现动态的给数组添加元素效果,实现对数组扩容。ArrayAdd.java
8+
1.原始数组使用静态分配 int[] arr = {1,2,3}
9+
2.增加的元素4,直接放在数组的最后 arr = {1,2,3,4}
10+
3.用户可以通过如下方法来决定是否继续添加,添加成功,是否继续?y/n
11+
12+
思路分析
13+
1. 定义初始数组 int[] arr = {1,2,3}//下标0-2
14+
2. 定义一个新的数组 int[] arrNew = new int[arr.length+1];
15+
3. 遍历 arr 数组,依次将arr的元素拷贝到 arrNew数组
16+
4. 将 4 赋给 arrNew[arrNew.length - 1] = 4;把4赋给arrNew最后一个元素
17+
5. 让 arr 指向 arrNew ; arr = arrNew; 那么 原来arr数组就被销毁
18+
*/
19+
int[] arr = {1,2,3};
20+
int[] arrNew = new int[arr.length + 1];
21+
//遍历 arr 数组,依次将arr的元素拷贝到 arrNew数组
22+
for(int i = 0; i < arr.length; i++) {
23+
arrNew[i] = arr[i];
24+
}
25+
//把4赋给arrNew最后一个元素
26+
arrNew[arrNew.length - 1] = 4;
27+
//让 arr 指向 arrNew,
28+
arr = arrNew;
29+
//输出arr 看看效果
30+
System.out.println("====arr扩容后元素情况====");
31+
for(int i = 0; i < arr.length; i++) {
32+
System.out.print(arr[i] + "\t");
33+
}
34+
35+
36+
37+
}
38+
}
39+
40+
41+

code/chapter06/ArrayAdd02.class

1.22 KB
Binary file not shown.

code/chapter06/ArrayAdd02.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.Scanner;
2+
public class ArrayAdd02 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
/*
7+
要求:实现动态的给数组添加元素效果,实现对数组扩容。ArrayAdd.java
8+
1.原始数组使用静态分配 int[] arr = {1,2,3}
9+
2.增加的元素4,直接放在数组的最后 arr = {1,2,3,4}
10+
3.用户可以通过如下方法来决定是否继续添加,添加成功,是否继续?y/n
11+
12+
思路分析
13+
1. 定义初始数组 int[] arr = {1,2,3}//下标0-2
14+
2. 定义一个新的数组 int[] arrNew = new int[arr.length+1];
15+
3. 遍历 arr 数组,依次将arr的元素拷贝到 arrNew数组
16+
4. 将 4 赋给 arrNew[arrNew.length - 1] = 4;把4赋给arrNew最后一个元素
17+
5. 让 arr 指向 arrNew ; arr = arrNew; 那么 原来arr数组就被销毁
18+
6. 创建一个 Scanner可以接受用户输入
19+
7. 因为用户什么时候退出,不确定,老师使用 do-while + break来控制
20+
*/
21+
22+
Scanner myScanner = new Scanner(System.in);
23+
//初始化数组
24+
int[] arr = {1,2,3};
25+
26+
do {
27+
int[] arrNew = new int[arr.length + 1];
28+
//遍历 arr 数组,依次将arr的元素拷贝到 arrNew数组
29+
for(int i = 0; i < arr.length; i++) {
30+
arrNew[i] = arr[i];
31+
}
32+
System.out.println("请输入你要添加的元素");
33+
int addNum = myScanner.nextInt();
34+
//把addNum赋给arrNew最后一个元素
35+
arrNew[arrNew.length - 1] = addNum;
36+
//让 arr 指向 arrNew,
37+
arr = arrNew;
38+
//输出arr 看看效果
39+
System.out.println("====arr扩容后元素情况====");
40+
for(int i = 0; i < arr.length; i++) {
41+
System.out.print(arr[i] + "\t");
42+
}
43+
//问用户是否继续
44+
System.out.println("是否继续添加 y/n");
45+
char key = myScanner.next().charAt(0);
46+
if( key == 'n') { //如果输入n ,就结束
47+
break;
48+
}
49+
}while(true);
50+
51+
System.out.println("你退出了添加...");
52+
}
53+
}

code/chapter06/ArrayAssign.class

976 Bytes
Binary file not shown.

code/chapter06/ArrayAssign.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
3+
public class ArrayAssign {
4+
5+
//编写一个main方法
6+
public static void main(String[] args) {
7+
8+
//基本数据类型赋值, 赋值方式为值拷贝
9+
//n2的变化,不会影响到n1的值
10+
int n1 = 10;
11+
int n2 = n1;
12+
13+
n2 = 80;
14+
System.out.println("n1=" + n1);//10
15+
System.out.println("n2=" + n2);//80
16+
17+
//数组在默认情况下是引用传递,赋的值是地址,赋值方式为引用赋值
18+
//是一个地址 , arr2变化会影响到 arr1
19+
int[] arr1 = {1, 2, 3};
20+
int[] arr2 = arr1;//把 arr1赋给 arr2
21+
arr2[0] = 10;
22+
23+
//看看arr1的值
24+
System.out.println("====arr1的元素====");
25+
for(int i = 0; i < arr1.length; i++) {
26+
System.out.println(arr1[i]);//10, 2, 3
27+
}
28+
29+
System.out.println("====arr2的元素====");
30+
for(int i = 0; i < arr2.length; i++) {
31+
System.out.println(arr2[i]);//10, 2, 3
32+
}
33+
34+
}
35+
}

code/chapter06/ArrayCopy.class

691 Bytes
Binary file not shown.

code/chapter06/ArrayCopy.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class ArrayCopy {
2+
3+
//编写一个main方法
4+
public static void main(String[] args) {
5+
6+
//将 int[] arr1 = {10,20,30}; 拷贝到 arr2数组,
7+
//要求数据空间是独立的.
8+
9+
int[] arr1 = {10,20,30};
10+
11+
//创建一个新的数组arr2,开辟新的数据空间
12+
//大小 arr1.length;
13+
int[] arr2 = new int[arr1.length];
14+
15+
//遍历 arr1 ,把每个元素拷贝到arr2对应的元素位置
16+
for(int i = 0; i < arr1.length; i++) {
17+
arr2[i] = arr1[i];
18+
}
19+
20+
//老师修改 arr2, 不会对arr1有影响.
21+
arr2[0] = 100;
22+
23+
//输出arr1
24+
System.out.println("====arr1的元素====");
25+
for(int i = 0; i < arr1.length; i++) {
26+
System.out.println(arr1[i]);//10,20,30
27+
}
28+
29+
//
30+
System.out.println("====arr2的元素====");
31+
for(int i = 0; i < arr2.length; i++) {
32+
System.out.println(arr2[i]);//
33+
}
34+
35+
}
36+
}

code/chapter06/ArrayDetail.class

772 Bytes
Binary file not shown.

code/chapter06/ArrayDetail.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
public class ArrayDetail {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
//1. 数组是多个相同类型数据的组合,实现对这些数据的统一管理
7+
8+
//int[] arr1 = {1, 2, 3, 60,"hello"};//String ->int
9+
double[] arr2 = {1.1, 2.2, 3.3, 60.6, 100};//int ->double
10+
11+
//2. 数组中的元素可以是任何数据类型,包括基本类型和引用类型,但是不能混用
12+
String[] arr3 = {"北京","jack","milan"};
13+
14+
//3. 数组创建后,如果没有赋值,有默认值
15+
//int 0,short 0, byte 0, long 0,
16+
//float 0.0,double 0.0,char \u0000,
17+
//boolean false,String null
18+
//
19+
short[] arr4 = new short[3];
20+
System.out.println("=====数组arr4=====");
21+
for(int i = 0; i < arr4.length; i++) {
22+
System.out.println(arr4[i]);
23+
}
24+
25+
//6. 数组下标必须在指定范围内使用,否则报:下标越界异常,比如
26+
//int [] arr=new int[5]; 则有效下标为 0-4
27+
//即数组的下标/索引 最小 0 最大 数组长度-1(4)
28+
int [] arr = new int[5];
29+
//System.out.println(arr[5]);//数组越界
30+
31+
}
32+
}

code/chapter06/ArrayExercise01.class

797 Bytes
Binary file not shown.

code/chapter06/ArrayExercise01.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
public class ArrayExercise01 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
7+
/*
8+
创建一个char类型的26个元素的数组,分别 放置'A'-'Z'。
9+
使用for循环访问所有元素并打印出来。
10+
提示:char类型数据运算 'A'+1 -> 'B'
11+
12+
思路分析
13+
1. 定义一个 数组 char[] chars = new char[26]
14+
2. 因为 'A' + 1 = 'B' 类推,所以老师使用for来赋值
15+
3. 使用for循环访问所有元素
16+
*/
17+
char[] chars = new char[26];
18+
for( int i = 0; i < chars.length; i++) {//循环26次
19+
//chars 是 char[]
20+
//chars[i] 是 char
21+
chars[i] = (char)('A' + i); //'A' + i 是int , 需要强制转换
22+
}
23+
24+
//循环输出
25+
System.out.println("===chars数组===");
26+
for( int i = 0; i < chars.length; i++) {//循环26次
27+
System.out.print(chars[i] + " ");
28+
}
29+
30+
}
31+
}

code/chapter06/ArrayExercise02.class

862 Bytes
Binary file not shown.

code/chapter06/ArrayExercise02.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
public class ArrayExercise02 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
7+
//请求出一个数组int[]的最大值 {4,-1,9, 10,23},并得到对应的下标
8+
//老韩思路分析
9+
//1. 定义一个int数组 int[] arr = {4,-1,9, 10,23};
10+
//2. 假定 max = arr[0] 是最大值 , maxIndex=0;
11+
//3. 从下标 1 开始遍历arr, 如果max < 当前元素,说明max 不是真正的
12+
// 最大值, 我们就 max=当前元素; maxIndex=当前元素下标
13+
//4. 当我们遍历这个数组arr后 , max就是真正的最大值,maxIndex最大值
14+
// 对应的下标
15+
16+
int[] arr = {4,-1,9,10,23};
17+
int max = arr[0];//假定第一个元素就是最大值
18+
int maxIndex = 0; //
19+
20+
for(int i = 1; i < arr.length; i++) {//从下标 1 开始遍历arr
21+
22+
if(max < arr[i]) {//如果max < 当前元素
23+
max = arr[i]; //把max 设置成 当前元素
24+
maxIndex = i;
25+
}
26+
}
27+
//当我们遍历这个数组arr后 , max就是真正的最大值,maxIndex最大值下标
28+
System.out.println("max=" + max + " maxIndex=" + maxIndex);
29+
}
30+
}

code/chapter06/ArrayReverse.class

904 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)