黑馬程序員Java零基礎視頻教程_上部(Java入門,含斯坦福大學練習題+力扣算

P197Set集合三種遍歷方式的實現(xiàn)

----------------華麗的分界線--------------------------

P108未看視頻先寫的,僅供參考

P110 字符串打亂,僅供參考

P110 驗證碼,數(shù)字隨機位置,僅供參考

P110字符串轉數(shù)字后的乘積

P114通過id查找對應的用戶信息,未開先寫,僅供參考

P115返回小于三千的手機信息,此處自己進行了拓展,較為復雜
接下來是學生管理系統(tǒng)代碼
JavaBean類我就不在此進行copy了
package com.itheima.studentmanagersystem;
import java.security.Provider;
import java.util.ArrayList;
import java.util.Scanner;
/**
?* @author XiangTao
?*/
public class StudentManagerSystem {
??public static void main(String[] args) {
????ArrayList<Student> list = new ArrayList<>();
????Scanner sc = new Scanner(System.in);
????//添加初始化數(shù)據(jù)
????Student s1 = new Student("heima001", "張三", 23, "南京");
????Student s2 = new Student("heima002", "李四", 24, "北京");
????Student s3 = new Student("heima003", "王五", 25, "深圳");
????list.add(s1);
????list.add(s2);
????list.add(s3);
????printMenu:
????while (true) {
??????printMenu();
??????String choose = sc.next();
??????switch (choose) {
????????case "1":
??????????add(list);
??????????break;
????????case "2":
??????????drop(list);
??????????break;
????????case "3":
??????????alter(list);
??????????break;
????????case "4":
??????????select(list);
??????????break;
????????case "5":
??????????System.out.println("退出成功!");
??????????break printMenu;
????????default:
??????????System.out.println("輸入錯誤,請重新輸入!");
??????????break;
??????}
????}
??}
??/**
???* @注釋 打印初始菜單
???*/
??public static void printMenu() {
????System.out.println("------歡迎來到黑馬學生管理系統(tǒng)------");
????System.out.println("1: 添加學生");
????System.out.println("2: 刪除學生");
????System.out.println("3: 修改學生");
????System.out.println("4: 查詢學生");
????System.out.println("5: 退出");
????System.out.println("請輸入您的選擇: ");
??}
??/**
???* @param list
???* @注釋 添加學生功能
???*/
??public static void add(ArrayList<Student> list) {
????Scanner sc = new Scanner(System.in);
????System.out.println("請輸入學生信息");
????System.out.println("請輸入id: ");
????String id = sc.next();
????boolean flag = constraintID(list, id);
????if (!flag) {
??????Student s = new Student();
??????while (true) {
????????System.out.println("請輸入姓名: ");
????????String name = sc.next();
????????boolean result = checkName(name);
????????if (!result) {
??????????System.out.println("姓名格式有誤,請重新輸入: ");
????????} else {
??????????System.out.println("請輸入年齡: ");
??????????int age = sc.nextInt();
??????????System.out.println("請輸入家庭住址: ");
??????????String address = sc.next();
??????????s.setId(id);
??????????s.setName(name);
??????????s.setAge(age);
??????????s.setAddress(address);
??????????list.add(s);
??????????System.out.println("添加成功");
??????????break;
????????}
??????}
????} else {
??????System.out.println(id + "已存在,請重新輸入");
????}
??}
??private static boolean checkName(String username) {
????//1.長度在3到15之間
????int len = username.length();
????if (len < 3 || len > 15) {
??????return false;
????}
????//2.必須是字母和數(shù)字的組合
????for (int i = 0; i < username.length(); i++) {
??????char c = username.charAt(i);
??????if (!(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
????????return false;
??????}
????}
????//3.不能是純數(shù)字
????int count = 0;
????for (int i = 0; i < username.length(); i++) {
??????char c = username.charAt(i);
??????if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
????????count++;
??????}
????}
????return count > 0;
??}
??/**
???* @param list
???* @param id
???* @return
???* @注釋 檢查添加時id是否唯一
???*/
??public static boolean constraintID(ArrayList<Student> list, String id) {
????return getIDIndex(list, id) >= 0;
??}
??/**
???* @param list
???* @param id
???* @return
???* @注釋 id存在返回索引, 否則返回-1
???*/
??public static int getIDIndex(ArrayList<Student> list, String id) {
????for (int i = 0; i < list.size(); i++) {
??????Student stu = list.get(i);
??????if (stu.getId().equals(id)) {
????????return i;
??????}
????}
????return -1;
??}
??/**
???* @param list
???* @注釋 刪除學生功能
???*/
??public static void drop(ArrayList<Student> list) {
????Scanner sc = new Scanner(System.in);
????System.out.println("請輸入需要刪除的學生ID: ");
????String id = sc.next();
????int index = getIDIndex(list, id);
????if (index != -1) {
??????list.remove(index);
??????System.out.println("刪除成功");
????} else {
??????System.out.println(id + "不存在");
????}
??}
??/**
???* @param list
???* @注釋 修改學生信息功能
???*/
??public static void alter(ArrayList<Student> list) {
????Scanner sc = new Scanner(System.in);
????Student s = new Student();
????System.out.println("請輸入需要修改的學生id: ");
????String id = sc.next();
????int index = getIDIndex(list, id);
????if (index >= 0) {
??????while (true) {
????????System.out.println("請輸入修改后的姓名: ");
????????String name = sc.next();
????????boolean result = checkName(name);
????????if (!result) {
??????????System.out.println("姓名格式有誤,請重新輸入: ");
????????} else {
??????????System.out.println("請輸入修改后的年齡: ");
??????????int age = sc.nextInt();
??????????System.out.println("請輸入修改后的家庭住址: ");
??????????String address = sc.next();
??????????//刪除原有數(shù)據(jù)
??????????list.remove(index);
??????????s.setId(id);
??????????s.setName(name);
??????????s.setAge(age);
??????????s.setAddress(address);
??????????list.add(s);
??????????System.out.println("修改成功");
??????????break;
????????}
??????}
????} else {
??????System.out.println(id + "不存在");
????}
??}
??/**
???* @param list
???* @注釋 查詢學生信息功能
???*/
??public static void select(ArrayList<Student> list) {
????if (list.size() == 0) {
??????System.out.println("當前無學生信息, 請?zhí)砑雍笤俨樵?#34;);
????}
????System.out.println("id\t\t姓名\t\t年齡\t\t家庭住址");
????for (int i = 0; i < list.size(); i++) {
??????Student s = list.get(i);
??????System.out.println(s.getId() + "\t" + s.getName() + "\t" +
??????????s.getAge() + "\t" + s.getAddress());
????}
??}
}
P122定義工具類ArrayUtil

P122定義工具類StudentUtil

P132多態(tài)的綜合練習,此處只供參考,只因未看先寫,只展示person類

P143匿名內部類的具體實現(xiàn)

P175第一題:

P177基礎查找課堂練習2

P178分塊查找(無規(guī)律的數(shù)據(jù))
