20220716 第二组 刘世琦 学习笔记
JAVA
(今日例题)
1. 员工管理系统(理解!会持续复习!)
2.数组
-
2.1 根据数组下标获取对应位置数字(掌握)
-
2.2 倒序输入数组数值(掌握)
-
2.3 倒序输入数组数值,扩容后数组仍倒序(掌握)
-
2.4 在数组某处插入数值(理解!会反复练习!)
1. 员工管理系统(理解!会持续复习!)
查询
思路:
把员工的姓名和工号保存到对应的数组里
有问题?
如果第一次输入的员工信息,保存到0的位置
第二次输入的员工信息,保存到1的位置
分析:
怎么才能知道即将要越界?
1.数组的最后一个元素不为null,0
2.当i的值和数组的最大下标,在进入到添加功能就先判断数组的下标是否合法
3.在添加成功之后,i自增之后,在判断下一次的数组的下标是否合法
修改
修改实际上是两个操作:
修改之前要先根据工号查询,是否在数组里
如果在,就执行修改的操作
如果不在,则显示当前用户不存在
删除
分析需求:
删除nos = 0,names = null
nos=0 names=null
移位
1,2,3,4,5
3删除掉
1,2,0,4,5
1,2,4,5,0
思路:
1,2,3,4,5 1,2,0,4,5 新建一个数组 把数组中的数据重新放进新的数组,不包括0 int [] arr = {1,2,3,4,5} int [] arr = {1,2,0,4,5} int [] newArr = new int[arr.length]; 删除:输入工号,先查询</font>
具体代码如下:
package com.practise;
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] nos = new int[2];
String [] names = new String[2];
// 初始的工号
int no = 1001;
// 要操作的数组的下标
int i = 0;
while(true){
System.out.println("欢迎使用员工管理系统");
System.out.println("请选择功能:1.添加员工 2.查询员工 3.修改员工 4.删除员工");
String flag = sc.next();
switch (flag) {
case "1":
System.out.println("请输入员工姓名:");
String name = sc.next();
System.out.println("添加成功:员工的工号为:" + nos[i] + ",姓名:" + names[i]);
i++;
no++;
if(i >= nos.length){
// 数组要扩容
int [] newNos = new int[nos.length + 1];
String [] newNames = new String[names.length + 1];
for (int j = 0; j < nos.length; j++) {
// 数组中的数据的复制
newNos[j] = nos[j];
newNames[j] = names[j];
}
// 重新赋值
nos = newNos;
names = newNames;
}
break;
case "2":
System.out.println("请选择功能:1、根据工号查询 2、查询所有");
String n = sc.next();
switch (n) {
case "1":
System.out.println("请输入要查询的员工号:");
int s = sc.nextInt();
// boolean b = false;
int index = -1;
for (int j = 0; j < nos.length; j++) {
if(nos[j] == s){
// 找到了
// System.out.println("工号:" + s + ",姓名:" + names[j]);
// b = true;
index = j;
break;
}
// else {
// System.out.println("工号:" + s + "不存在!");
// }
}
if(index!= -1) {
System.out.println("工号:" + s + ",姓名:" + names[index]);
}else {
System.out.println("工号:" + s + "不存在!");
}
break;
case "2":
for (int j = 0; j < nos.length; j++) {
if(nos[j] != 0){
System.out.println("工号:" + nos[j] + ",姓名:" + names[j]);
}
}
break;
default:
}
break;
case "3":
System.out.println("请输入要修改的工号:");
int x = sc.nextInt();
int index = -1;
for (int j = 0; j < nos.length; j++) {
if(nos[j] == x){
index = j;
break;
}
}
if(index!= -1) {
System.out.println("工号:" + x + ",姓名:" + names[index]);
System.out.println("请输入新的姓名:");
String newName = sc.next();
names[index] = newName;
System.out.println("修改成功!工号:" + x + ",姓名:" + names[index]);
}else {
System.out.println("工号:" + x + "不存在!");
}
break;
case "4":
int [] arr = {1,2,3,4,5}
int [] arr = {1,2,0,4,5}
int [] newArr = new int[arr.length];
System.out.println("请输入要查询的员工号:");
int s = sc.nextInt();
// 先要去工号的数组中找,如果工号存在,拿到工号对应的下标
int y = -1;
for (int j = 0; j < nos.length; j++) {
if(nos[j] == s){
y = j;
break;
}
}
if(y!= -1) {
System.out.println("工号:" + s + ",姓名:" + names[y]);
// 找到了,就删除
nos[y] = 0;
names[y] = null;
int [] newNos = new int[nos.length - 1];
String [] newNames = new String[names.length - 1];
for (int j = 0; j < nos.length - 1; j++) {
if(nos[j] == 0){
newNos[j] = nos[j + 1];
nos[j + 1] = 0;
}else {
newNos[j] = nos[j];
}
if(names[j] == null){
newNames[j] = names[j + 1];
names[j + 1] = null;
}else {
newNames[j] = names[j];
}
}
nos = newNos;
names = newNames;
System.out.println("工号:" + s + "删除成功!");
}else {
System.out.println("工号:" + s + "不存在!");
}
break;
default:
}
}
}
}
2.数组
-
2.1 根据数组下标获取对应位置数字(掌握)
有一个数组
键盘输入一个下标,获取指定下标位置的数字
输入的这个数是否合法?
需要判断几种情况?
1.输入的是一个负值
2.输入的是一个正值,越界了
3.正确的情况
package com.practise;
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] arr = new int[]{4,58,69,14,22,-1};
System.out.println("请输入一个下标:");
int index = sc.nextInt();
if(index >= 0 && index <= arr.length - 1){
// 合法的情况
System.out.println("对应位置的值是:" + arr[index]);
} else {
// 条件 <0 || >5
// 越界
System.out.println("你输入的数据有误,无法获取...");
}
}
}
2.2 倒序输入数组数值(掌握)
输入一个数,数是从后往前加
倒序输入
第一次输入的值给arr[arr.length - 1 - 0]
第二次输入的值给arr[arr.length - 1 - 1]
第三次输入的值给arr[arr.length - 1 - 2]
数组的最大下标 = 数组的长度 - 1
package com.practise;
import java.util.Scanner;
public class Ch02 {
public static void main(String[] args) {
// 准备一个键盘
Scanner sc = new Scanner(System.in);
int [] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
System.out.println("请输入第" + (i+1) +"个数:");
// 输入
int num = sc.nextInt();
arr[arr.length - 1 - i] = num;
}
for (int i : arr) {
System.out.println(i);
}
}
}
2.3 倒序输入数组数值,扩容后数组仍倒序(掌握)
输入1,2
[2,1]
[0,0,0,0]
[2,1,0,0]
[0,0,2,1]
package com.practise;
import java.util.Arrays;
import java.util.Scanner;
public class Ch03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] arr = new int[2];
int i = 0;
main:while(true){
System.out.println("请输入数据:");
int num = sc.nextInt();
arr[arr.length - 1 - i] = num;
i++;
if(i >= arr.length) {
int [] newArr = new int[arr.length * 2];
for (int j = 0; j < arr.length; j++) {
newArr[newArr.length - 1 - j] = arr[arr.length - 1 - j];
}
arr = newArr;
}
System.out.println("是否继续添加:1、是 2、否");
String flag = sc.next();
switch (flag){
case "1":
continue;
case "2":
System.out.println("当前数组为:" + Arrays.toString(arr));
break main;
// System.exit(-1);
}
}
}
}
2.4 在数组某处插入数值(理解!会反复练习!)
1.因为数组已满,需要扩容
2.找到要插入的数据的位置
3.插入位置后面的数据需要向后移位
定义一个临时变量,存储原来index位置的元素
int temp = arr[index];
把要插入的数存到了指定的位置
arr[index] = num;
原来位置的后面的那个数向后移一位
arr[index + 2] = arr[index + 1];
arr[index + 1] = temp;
要插入的位置的原始数据保存起来
int temp = arr[index];
把要插入的数据放到指定位置
arr[index] = num;
判断要移位的数据的个数
package com.practise;
import java.util.Arrays;
import java.util.Scanner;
public class Ch04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] arr = new int[]{1,2,3,4,5};
// 扩容
int [] newArr = new int[arr.length * 2];
for (int j = 0; j < arr.length; j++) {
newArr[j] = arr[j];
}
arr = newArr;
main:while(true){
System.out.println("请输入要插入数据的下标:");
int index = sc.nextInt();
System.out.println("请输入要插入的数据:");
int num = sc.nextInt();
int temp = arr[index];
arr[index] = num;
arr[index + 2] = arr[index + 1];
arr[index + 1] = temp;
int temp = arr[index];
arr[index] = num;
//判断要移位的数据的个数
for (int i = 1; i < arr.length - index; i++) {
arr[arr.length - i] = arr[arr.length - 1 - i];
}
arr[index + 1] = temp;
System.out.println("现在数组为:" + Arrays.toString(arr));
System.out.println("是否继续添加:1、是 2、否");
String flag = sc.next();
switch (flag){
case "1":
continue;
case "2":
System.out.println("当前数组为:" + Arrays.toString(arr));
break main;
// System.exit(-1);
}
}
}
}
总结
今天老师的讲解很大部分都是我在昨天作业中进行不下去的地方,有一种豁然开朗的感觉,现在还在对代码的理解阶段,无法独立编写。在数组练习中,会有理解不到位的情况,但是我发现我对于题目的需求分析比之前更加全面,分析能力在一天天的学习中有所提升,每一天都会成为一个全新的自己,充满着热情迎接每一天的挑战,较为顺利的完成今天的数组作业。心情非常好,不仅仅是因为解决了存在令我存疑的问题,更是发现自己会比前一天有所进步,做到当天的事情当天解决,不要存在问题拖延的情况,我会让努力将自己变的更好!