第四天,面向过程阶段快要过去了,继续加油吧
一、练习
改进人员名单管理系统,增加人员编号,增加删除人员功能,增加ID查找,姓氏查找,模糊查找等功能:
import java.util.Arrays;
import java.util.Scanner;
public class Employee {
static String[] employee = { "1-jack", "2-lucy", "3-rose" };
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("------欢迎进入员工管理系统------");
while (true) {
welcome();
}
}
// 展示主菜单
public static void welcome() {
System.out.println("------主 菜 单------");
System.out.println("-0.退出");
System.out.println("-1.查询全部人员");
System.out.println("-2.增加新的人员");
System.out.println("-3.删除已有人员");
System.out.println("-4.根据ID查找人员");
System.out.println("-5.按姓氏或首字母筛选");
System.out.println("-6.模糊查找");
System.out.println("#号键为返回主菜单!");
System.out.println("---------------------");
String sIn = scan.next();
if ("0".equals(sIn)) {
System.out.println("-正在退出,请稍后...");
System.exit(0);
} else if ("1".equals(sIn)) {
showEmployee();
} else if ("2".equals(sIn)) {
addEmployee();
} else if ("3".equals(sIn)) {
delEmployee();
} else if ("4".equals(sIn)) {
searchByID();
} else if ("5".equals(sIn)) {
showEmployeeByFirstChar();
} else if ("6".equals(sIn)) {
showEmployeeByContain();
} else if ("#".equals(sIn)) {
System.out.println();
} else {
System.out.println("非法输入");
System.out.println();
}
}
// 展示人员
public static void showEmployee() {
System.out.println("------人员名单------");
for (String name : employee) {
System.out.println("\t" + name);
}
System.out.println("---------------------");
System.out.println();
}
// 添加人员
public static void addEmployee() {
System.out.println("-请输入一个人员姓名来添加");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
String newMember = getNextID(employee) + "-" + sIn;
employee = addToArr(newMember, employee);
System.out.println("-输入成功!");
System.out.println();
}
// 删除人员
public static void delEmployee() {
System.out.println("-请输入一个人员ID来删除");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
int index = IDToIndex(sIn, employee);
if (index == -1) {
System.out.println("-未找到或输入有误");
} else {
employee = delFromArr(index, employee);
System.out.println("-删除成功!");
}
System.out.println();
}
// 按ID搜索成员
public static void searchByID() {
System.out.println("-请输入一个人员ID来查找");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
int index = IDToIndex(sIn, employee);
if (index == -1) {
System.out.println("-未找到或输入有误");
} else {
System.out.println("------人员名单(ID查找)------");
System.out.println("\t" + employee[IDToIndex(sIn, employee)]);
System.out.println("------------------------------");
}
System.out.println();
}
// 姓或首字母筛选
public static void showEmployeeByFirstChar() {
System.out.println("-请输入姓氏或首字母");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
String[] arr = containArray(sIn, employee);
if (arr == null) {
System.out.println("-未找到相关信息");
} else {
arr = firstArray(sIn.charAt(0), arr);
if (arr == null) {
System.out.println("-未找到相关信息");
} else {
System.out.println("------人员名单(姓氏或首字母查找)------");
for (String name : arr) {
System.out.println("\t" + name);
}
System.out.println("-----------------------------------------");
}
}
System.out.println();
}
// 模糊查找
public static void showEmployeeByContain() {
System.out.println("-请输入关键字");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
String[] arr = containArray(sIn, employee);
if (arr == null) {
System.out.println("-未找到相关信息");
} else {
System.out.println("------人员名单(模糊查找)------");
for (String name : arr) {
System.out.println("\t" + name);
}
System.out.println("--------------------------------");
}
System.out.println();
}
// 扩容
public static String[] addToArr(String member, String[] memberArr) {
String[] newMemberArr = Arrays.copyOf(memberArr, memberArr.length + 1);
newMemberArr[newMemberArr.length - 1] = member;
return newMemberArr;
}
// 删除
public static String[] delFromArr(int index, String[] memberArr) {
for (int i = index; i < memberArr.length - 1; i++) {
memberArr[i] = memberArr[i + 1];
}
String[] newMemberArr = Arrays.copyOf(memberArr, memberArr.length - 1);
return newMemberArr;
}
// 筛选数组
public static String[] containArray(String part, String[] arr) {
String[] newArr = {};
for (String member : arr) {
if (member.contains(part)) {
newArr = addToArr(member, newArr);
}
}
if (newArr.length == 0) {
return null;
} else {
return newArr;
}
}
// 按首字母筛选数组
public static String[] firstArray(char ch, String[] arr) {
String[] newArr = {};
for (String member : arr) {
if (ch == member.split("-")[1].charAt(0)) {
newArr = addToArr(member, newArr);
}
}
if (newArr.length == 0) {
return null;
} else {
return newArr;
}
}
// 获取下一个ID
public static int getNextID(String[] arr) {
int maxID = 0;
for (String member : arr) {
if (member == null) {
continue;
}
int memberID = Integer.parseInt(member.split("-")[0]);
if (maxID < memberID) {
maxID = memberID;
}
}
return maxID + 1;
}
// 根据ID获取下标
public static int IDToIndex(String ID, String[] arr) {
for (int i = 0; i < arr.length; i++) {
if (ID.equals(arr[i].split("-")[0])) {
return i;
}
}
return -1;
}
}
运行结果:
System.exit(0);
这句是用于关闭虚拟机,结束程序运行。
二、ArrayList
ArrayList 是集合框架中较为常用一种结构,通常被称为数组表,也可以叫做动态数组。它的底层是基于数组实现的,继承自 AbstractList,实现了 List 接口。与传统数组相比,它能实现容量大小的动态变化。
2.1 创建 ArrayList
数组表需要通过声明一个ArrayList的对象来创建,其格式:
ArrayList<Object> list = new ArrayList<Object>();
数组表的声明过程中,<>内可以填任意的类名,来创建一个该类的集合。
2.2 包装类
因为八大基本数据类型都不属于类,所以不能直接声明基本数据类型的集合,因此引入了包装类的概念。每一个基本数据类型都有一个对应的包装类,包装类除了可以等于null以外,其他功能与基本类型一致。
八大基本数据类型及其对应的包装类:
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
boolean | Boolean |
char | Character |
float | Float |
double | Double |
2.3 ArrayList 常用 API
API | 功能 | 输出类型 |
---|---|---|
add(Object) | 添加一个成员 | boolean |
add(int,Object) | 在指定位置添加一个成员 | void |
set(int,Object) | 将指定位置成员替换为给定成员 | Object |
remove(Object) | 删除一个成员 | boolean |
remove(int) | 删除指定下标的成员 | Object |
clear() | 清空集合 | void |
get(int) | 获取该位置的成员 | Object |
contains(Object) | 包含 | boolean |
isEmpty() | 是否为空 | boolean |
indexOf(Object) | 获取某个成员第一个下标 | int |
lastInsexOf(Object) | 获取某个成员最后一个下标 | int |
size() | 长度 | int |
toArray() | 转化为数组 | Object[] |
toString() | 转化为字符串 | String |
输出全部常用API:
import java.util.ArrayList;
public class ArrayListAPI {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("123");
list.add("abc");
list.add("ABC");
System.out.println(list.toString());
list.remove("abc");
System.out.println(list.toString());
list.add(1, "456");
System.out.println(list.toString());
list.remove(1);
System.out.println(list.toString());
list.set(0, "789");
System.out.println(list.toString());
String indexMember = list.get(0);
int memberIndex = list.indexOf("789");
boolean containsABC = list.contains("ABC");
boolean isEmpty = list.isEmpty();
int listSize = list.size();
System.out.println(indexMember);
System.out.println(memberIndex);
System.out.println(containsABC);
System.out.println(isEmpty);
System.out.println(listSize);
}
}
运行结果:
2.4 练习
用数组表来改进人员名单管理系统:
import java.util.ArrayList;
import java.util.Scanner;
public class Employee {
static ArrayList<String> employee = new ArrayList<String>();
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
employee.add("1-jack");
employee.add("2-lucy");
employee.add("3-rose");
System.out.println("------欢迎进入员工管理系统------");
while (true) {
welcome();
}
}
// 展示主菜单
public static void welcome() {
System.out.println("------主 菜 单------");
System.out.println("-0.退出");
System.out.println("-1.查询全部人员");
System.out.println("-2.增加新的人员");
System.out.println("-3.删除已有人员");
System.out.println("-4.根据ID查找人员");
System.out.println("-5.按姓氏或首字母筛选");
System.out.println("-6.模糊查找");
System.out.println("#号键为返回主菜单!");
System.out.println("---------------------");
String sIn = scan.next();
if ("0".equals(sIn)) {
System.out.println("-正在退出,请稍后...");
System.exit(0);
} else if ("1".equals(sIn)) {
showEmployee();
} else if ("2".equals(sIn)) {
addEmployee();
} else if ("3".equals(sIn)) {
delEmployee();
} else if ("4".equals(sIn)) {
searchByID();
} else if ("5".equals(sIn)) {
showEmployeeByFirstChar();
} else if ("6".equals(sIn)) {
showEmployeeByContain();
} else if ("#".equals(sIn)) {
System.out.println();
} else {
System.out.println("非法输入");
System.out.println();
}
}
// 展示人员
public static void showEmployee() {
System.out.println("------人员名单------");
for (String name : employee) {
System.out.println("\t" + name);
}
System.out.println("---------------------");
System.out.println();
}
// 添加人员
public static void addEmployee() {
System.out.println("-请输入一个人员姓名来添加");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
String newMember = getNextID(employee) + "-" + sIn;
employee.add(newMember);
System.out.println("-输入成功!");
System.out.println();
}
// 删除人员
public static void delEmployee() {
System.out.println("-请输入一个人员ID来删除");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
int index = IDToIndex(sIn, employee);
if (index == -1) {
System.out.println("-未找到或输入有误");
} else {
employee.remove(index);
System.out.println("-删除成功!");
}
System.out.println();
}
// 按ID搜索成员
public static void searchByID() {
System.out.println("-请输入一个人员ID来查找");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
int index = IDToIndex(sIn, employee);
if (index == -1) {
System.out.println("-未找到或输入有误");
} else {
System.out.println("------人员名单(ID查找)------");
System.out.println("\t" + employee.get(index));
System.out.println("------------------------------");
}
System.out.println();
}
// 姓或首字母筛选
public static void showEmployeeByFirstChar() {
System.out.println("-请输入姓氏或首字母");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
ArrayList<String> list = containArray(sIn, employee);
if (!list.isEmpty()) {
list = firstArray(sIn.charAt(0), list);
}
if (list.isEmpty()) {
System.out.println("-未找到相关信息");
} else {
System.out.println("------人员名单(姓氏或首字母查找)------");
for (String name : list) {
System.out.println("\t" + name);
}
System.out.println("-----------------------------------------");
}
System.out.println();
}
// 模糊查找
public static void showEmployeeByContain() {
System.out.println("-请输入关键字");
String sIn = scan.next();
if ("#".equals(sIn)) {
System.out.println();
return;
}
ArrayList<String> list = containArray(sIn, employee);
if (list.isEmpty()) {
System.out.println("-未找到相关信息");
} else {
System.out.println("------人员名单(模糊查找)------");
for (String name : list) {
System.out.println("\t" + name);
}
System.out.println("--------------------------------");
}
System.out.println();
}
// 筛选数组
public static ArrayList<String> containArray(String part, ArrayList<String> list) {
ArrayList<String> newList = new ArrayList<String>();
for (String member : list) {
if (member.contains(part)) {
newList.add(member);
}
}
return newList;
}
// 按首字母筛选数组
public static ArrayList<String> firstArray(char ch, ArrayList<String> list) {
ArrayList<String> newList = new ArrayList<String>();
for (String member : list) {
if (ch == member.split("-")[1].charAt(0)) {
newList.add(member);
}
}
return newList;
}
// 获取下一个ID
public static int getNextID(ArrayList<String> list) {
int maxID = 0;
for (String member : list) {
if (member == null) {
continue;
}
int memberID = Integer.parseInt(member.split("-")[0]);
if (maxID < memberID) {
maxID = memberID;
}
}
return maxID + 1;
}
// 根据ID获取下标
public static int IDToIndex(String ID, ArrayList<String> list) {
for (int i = 0; i < list.size(); i++) {
if (ID.equals(list.get(i).split("-")[0])) {
return i;
}
}
return -1;
}
}
运行结果: