2022-7-20 第一小组 甘源册 学习笔记
知识点掌握情况
String字符串(了解) 包装器类型(理解)
学习心情(心路历程)
知识点比较简单,学起来很轻松,Nice!!!
1. String字符串
-
String str="aknk"; String s1="aknk"; String s = new String("aknk"); //s指向的aknk与s1指向的aknk不在同一个区域内 String s2 = new String("aknk"); System.out.println(s2==s); // false System.out.println(s1==s); // false System.out.println(s1==str);// true -
== 双等号比较的是虚地址
-
虚地址:对象在内存中的存储位置
-
String字符串——字符拼成的串
-
String一旦声明不可改变
1.1 String常用方法
-
equals方法(比较字符串的内容)
- 需要传参,传String类型的参数。
- 有返回值,返回值是布尔类型的。
- 访问权限public
-
length方法(获取字符串的长度)
- 不需要传参
- 有返回值,返回值是整数型(int)的。
- 访问权限public
- 数组的length是属性,字符串的length()是方法
-
charAt方法(取出指定下标位置的字符)
- 需要传参,传int类型的参数
- 有返回值,返回字符型的。
- 访问权限public
-
indexOf(String str)(判断指定字符是否存在,返回值为字符串的下标)
- 需要传参
- 有返回值:int
- 如果不存在返回-1
-
indexOf(String str, int fromIndex)(判断指定字符是否存在,从int的位置开始找,包括当前位置,从前往后)
-
lastIndexOf(String str, int fromIndex)(意思同index'Of一样,从后往前找)
-
substring方法(截取成一个新的字符串)
- 传一个int参数——截取从int下标开始到最后的所有字符 (包含起始位置)
- 传两个int参数——截取从第一个int下标到第二个int下标之间的所有字符串(不包括终止位置的字符)
-
不重要
-
String s1="aas,ca,cfs,aga,c,aa"; String s2="AAS"; s1.toUpperCase(); //转大写 s1.toLowerCase(); //转小写 s1.startsWith("a",2); //判断以什么开头 返回布尔型 s1.endsWith("a"); //判断以什么结尾 返回布尔型 s1.equalsIgnoreCase(s2);//忽略大小写进行比较 s1.trim(); // 去掉字符串前后的空格 String[] str=s1.split(",");//根据指定的字符分割
-
-
replace方法(字符串的替换)
-
String s1="adasggasda"; System.out.println(s1.replace("a", ""));//dsggsd System.out.println(s1.replaceAll("ada", ""));//sggasda
-
-
任何数据类型和字符串做加法,结果都是字符串
-
把其他数据类型转换成字符串,(+“”)不推荐,(valueOf(基本数据类型))推荐
-
字符串转换成数组
-
//转成字节型的数组 //操作文件IO流的时候用到 private static void test3(String s1) { byte[] bytes=s1.getBytes(); for (byte b : bytes) { System.out.println(b); } }
-
2. 包装器类型
| 基本数据类型 | byte | short | int | long | float | double | char | boolean |
|---|---|---|---|---|---|---|---|---|
| 包装类(封装类) | Byte | Short | Integer | Long | Float | Double | Character | Boolean |
以后不要用基本数据类型
-
Integer的默认值是null,int的默认值是0
-
/* 原理: 把int类型包装成包装器的Ingeter类型 * */ static Integer a =1; -
把基本数据类型转换成对应的包装器类型——自动装箱
-
把包装器类型转换成对应的基本数据类型——自动拆箱
2.1 包装器类型的方法
- parseInt方法(把字符串转换成int类型)
- toString方法(把基本数据类型转换成字符串类型)
3.拓展
-
JDK5之后的新功能:
- 自动装箱,自动拆箱
- 增强for循环(foreach)
- 枚举
-
JDK7以后的新功能:
- switch...case可以用字符串
-
异常:
- 数字格式化异常
- 字符串下标越界异常
练习
-
需求:查找在一个字符串中另一个字符串出现的次数,
-
int c=0; for (int i = 0; i < s1.length(); ) { if (s1.indexOf(s2,i)>-1){ i=s1.indexOf(s2,i)+1; c++; }else{ i++; } } System.out.println(c); -
需求:根据身份证号码获取生日,性别
-
private static void test5(Scanner scanner) { System.out.println("请输入您的身份证号码:"); String next = scanner.next(); if (next.length()==18) { String month = next.substring(10, 12); String year = next.substring(6, 10); String day = next.substring(12, 14); String gender = next.substring(16, 17); int age = Integer.parseInt(year); int a = Integer.parseInt(gender); System.out.println("年龄:" + (2022 - age)); System.out.println("出生日期:" + year + "年" + month + "月" + day + "日"); if (a % 2 == 0) { System.out.println("性别:女"); } else { System.out.println("性别:男"); } }else { test5(scanner); } }

员工信息管理系统
User类
package com.gyc.afternoon;
public class User {
String username;
String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
EmpLoyee类
package com.gyc.afternoon;
public class EmpLoyee {
String name;
int id;
public EmpLoyee() {
}
public EmpLoyee(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
UserManager类(工具类)
package com.gyc.afternoon;
public class UserManager {
Dem0 dem0 = new Dem0();
private User[] users = new User[2];
User[] users1 = new User[2];
EmpLoyee[] employees=new EmpLoyee[1];
EmpLoyee[] employees1=new EmpLoyee[1];
private Integer index=1;
private Integer id=1001;
private Integer i1=0;
EmpLoyee empLoyee;
User user;
public UserManager() {
users[0]=new User("admin","123456");
}
public EmpLoyee[] getEmployees() {
return employees;
}
public void setEmployees(EmpLoyee[] employees) {
this.employees = employees;
}
public User[] getUsers() {
return users;
}
public void setUsers(User[] users) {
this.users = users;
}
//注册
public String register(String password,String username){
users=dem0.kuo(users,users1);
user = new User();
user.setPassword(password);
user.setUsername(username);
users[index] = user;
this.index=this.index+1;
return "账户:" + username + "密码:" + password;
}
//添加员工
public String register1(String name){
employees=dem0.kuo(employees,employees1);
empLoyee= new EmpLoyee();
empLoyee.setId(id);
empLoyee.setName(name);
employees[id-1001] = empLoyee;
this.id=this.id+1;
return "员工工号:" + empLoyee.getId() + "姓名:" + name;
}
//登录
public boolean login(String username1,String password1){
for (int i = 0; i < users.length; i++) {
if (users[i]!=null) {
if (username1.equals(users[i].getUsername()) && password1.equals(users[i].getPassword())) {
return true;
}
}
}
return false;
}
//工号查询
public EmpLoyee select(int ind ){
for (int i = 0; i < employees.length; i++) {
if (employees[i]!=null) {
if (ind == employees[i].getId()) {
return employees[i];
}
}
}
return null;
}
//全部查询
public void each1(EmpLoyee[] empLoyees){
System.out.print("工号--");
System.out.print("姓名--");
System.out.println();
for (EmpLoyee loyee : empLoyees) {
if (loyee!=null){
System.out.print(loyee.getId()+"--");
System.out.print(loyee.getName()+"--");
System.out.println();
}
}
}
//删除
public String delete(int id1){
for ( i1 = 0; i1 < employees.length; i1++) {
if (employees[i1]!=null) {
if (id1 == employees[i1].getId()) {
employees[i1]=null;
yw(employees,i1);
EmpLoyee[] uu=new EmpLoyee[employees.length-1];
for (int i1 = 0; i1 < employees.length-1; i1++) {
uu[i1]=employees[i1];
}
employees=uu;
dem0.bl(employees);
return "删除成功";
}
}
}
return null;
}
//移位
public void yw(EmpLoyee[] empLoyees,int ii){
for(int a = ii+1;a<empLoyees.length-1 ;a++){//开始移位
EmpLoyee u1 = null;
u1 = empLoyees[a];
empLoyees[a] = empLoyees[a+1];
empLoyees[a+1] = u1;
}
for (int i = ii+1; i < empLoyees.length; i++) {
empLoyees[i].setId(empLoyees[i].getId()-1);
}
empLoyees[ii] = empLoyees[empLoyees.length-1];
empLoyees[empLoyees.length-1]=null;
System.out.println(empLoyees.length);
this.id=this.id-1;
}
//修改
public String update(String name,Integer i2){
for (int i = 0; i < employees.length; i++) {
if (employees[i]!=null) {
if (i2==employees[i].getId()) {
employees[i].setName(name);
return "修改成功";
}
}
}
return "修改失败";
}
}
Dem0类(面板类)
package com.gyc.afternoon;
import java.util.Scanner;
public class Dem0 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
UserManager userManager = new UserManager();
Dem0 dem0 = new Dem0();
a:
for (; ; ) {
User[] users=userManager.getUsers();
System.out.println("请选择功能 1:注册 2:登录 3:查询");
String flag = scanner.next();
switch (flag) {
case "1":
System.out.println("请输入账户:");
String username = scanner.next();
boolean b = false;
c:
for (int i = 0; i < users.length; i++) {
if (users[i]==null) {
continue c;
}
if (username.equals( users[i].getUsername())) {
b = true;
break;
}
}
if (b == false) {
System.out.println("请输入密码:");
String password = scanner.next();
String register = userManager.register(password, username);
System.out.println(register);
continue a;
} else {
System.out.println("用户名重复");
continue a;
}
case "2":
b:
for (; ; ) {
System.out.println("请输入账户:");
String username1 = scanner.next();
System.out.println("请输入密码:");
String password1 = scanner.next();
boolean login = userManager.login(username1, password1);
if (login == true){
EmpLoyee[] empLoyees=userManager.getEmployees();
System.out.println("登陆成功!");
x:for (;;) {
System.out.println("请选择功能: 1:添加员工 2:查询员工 3:修改员工 4:删除员工");
String next = scanner.next();
switch (next) {
case "1":
System.out.println("请输入姓名");
String next1 = scanner.next();
String s = userManager.register1(next1);
System.out.println(s);
continue x;
case "2":
System.out.println("请选择查询功能 1:工号查询 2:全部查询");
int anInt3 = scanner.nextInt();
switch (anInt3) {
case 1:
System.out.println("请输入工号:");
int anInt = scanner.nextInt();
EmpLoyee select = userManager.select(anInt);
if (select != null) {
System.out.println("员工工号:" + select.getId() + "姓名:" + select.getName());
System.out.println("查询成功");
continue x;
}
System.out.println("查询失败");
continue;
case 2:
userManager.each1(userManager.getEmployees());
continue x;
}
case "3":
System.out.println("请输入工号:");
int anInt1 = scanner.nextInt();
EmpLoyee select1 = userManager.select(anInt1);
if (select1 != null) {
System.out.println("请输入新的姓名");
String next2 = scanner.next();
System.out.println(userManager.update(next2,anInt1));
continue x;
}
System.out.println("查询失败");
continue ;
case "4":
System.out.println("请输入您要删除的工号:");
int anInt2 = scanner.nextInt();
String delete = userManager.delete(anInt2);
if (delete != null) {
System.out.println(delete);
continue x;
}
System.out.println("删除失败");
continue ;
default:
break;
}
continue a;
}
}
System.out.println("账户或密码错误,请重新输入");
continue b;
}
case "3":
dem0.bl(users);
break a;
default:
continue a;
}
}
}
//User[]数组扩容
public User[] kuo(User[] users, User[] users1) {
while (users[users.length - 1] != null) {
users1 = new User[users.length + 1];
for (int i = 0; i < users.length; i++) {
users1[i] = users[i];
}
users = users1;
}
return users;
}
//遍历User[]数组
public void bl(User[] arr) {
for (User user : arr) {
System.out.println("账户:" + user.getUsername() + "密码:" + user.getPassword());
}
}
//遍历EmpLyoee[]数组
public void bl(EmpLoyee[] arr) {
for (EmpLoyee user : arr) {
System.out.println("姓名:" + user.getName() + "工号:" + user.getId());
}
}
//EmpLyoee[]数组
public EmpLoyee[] kuo(EmpLoyee[] users, EmpLoyee[] users1) {
while (users[users.length - 1] != null) {
users1 = new EmpLoyee[users.length + 1];
for (int i = 0; i < users.length; i++) {
users1[i] = users[i];
}
users = users1;
}
return users;
}
}


浙公网安备 33010602011771号