-
经验与总结
查看全部 -
package com.kandy.imoockecheng.listmap; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class StringSort { public static void main(String[] args) { /* * 随机生成10条长度10以内,不可重复的字符串,每条字符串内的字符可以重复 * 为节省String消耗,全部使用 StringBuilder */ //定义字符串条数 int count = 10; //定义最大字符串的长度 int maxLength = 10; //定义备选字符N个 StringBuilder stringBuilder = new StringBuilder("ABCDEFGHIJGLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"); //定义被选出来的字符串 StringBuilder stringSel = new StringBuilder(); //创建字符串List集合 List<String> strList = new ArrayList<>(); //do循环控制字符串的条数 do { //随机字符串的长度(10以内) int rdLong = new Random().nextInt(maxLength); //for循环随机拼凑字符的和长度 for (int i = 0; i < rdLong; i++) { //随机获取备选字符串某个字符的索引位置 int index = new Random().nextInt(stringBuilder.length()); //按索引位置选出一个字符,把随机生成的字符追加到stringSel stringSel.append(stringBuilder.charAt(index)); } //如果包含字符串,重新生成随机字符串 if(strList.contains(stringSel.toString())) continue; //把随机生成的字符串添加到strList中 strList.add(stringSel.toString()); //清空stringSel stringSel.delete(0,stringSel.length()-1); } while (strList.size() != count); //10条后跳出do循环 System.out.println("*******排序前************"); strList.forEach(System.out::println); //进行排序 Collections.sort(strList); System.out.println("*******排序后************"); strList.forEach(System.out::println); } }
查看全部 -
处理异常
try-catch以及try-catch-finally处理异常
需要注意顺序
查看全部 -
//简约版2020 package com.kandy.imoockecheng.listmap; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class TestMap { Map<String,Student> students; public TestMap(){ this.students = new HashMap<>(); } public void testPut(){ Scanner input = new Scanner(System.in); System.out.println("开始录入学生信息:"); while (true){ System.out.print("请输入学生ID,输入0退 : "); String id = input.next(); //判断是否退出 if(id.equals("0")){ input.close(); break; } //判断是否存在学生的id Student st = students.get(id); if(st!=null) { System.out.println(">>已存在的ID,请重新输入!"); continue; } //提示输入学生的姓名 System.out.print("请输入学生的姓名:"); String name = input.next(); //创建学生对象 Student student = new Student(id,name); //添加的students 到map中 students.put(id,student); System.out.println("添加完成!可以继续添加."); } } //查看学生列表 public void showStudents(){ System.out.println(">>>>>>>学生列表<<<<<<<"); System.out.println("ID\t\t姓名"); students.forEach((k,student)->{ System.out.println(k+"\t\t"+student.name); }); } public static void main(String[] args) { TestMap tm = new TestMap(); tm.testPut(); tm.showStudents(); } }
查看全部 -
package com.kandy.imoockecheng.listmap; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class TestGeneric { List<Course> list; public TestGeneric(){ //使用前需要实例化 this.list = new ArrayList<Course>(); } //添加元素 public void initialList(){ Course[] courses = { new Course("1","C++"), new Course("2","Java") }; list.addAll(Arrays.asList(courses)); showList(); } //显示list public void showList(){ list.forEach(course -> { System.out.println(course.id+":"+course.name); }); } public static void main(String[] args) { TestGeneric tg=new TestGeneric(); tg.initialList(); } } //课程类 package com.kandy.imoockecheng.listmap; public class Course { public String id; public String name; public Course(String id,String name){ this.id=id; this.name=name; } }
查看全部 -
package com.kandy.imoockecheng.listmap; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class Test { //创建备选课程的集合 List courseList = new ArrayList(); //添加课程到备选课程的集合中 public void initCourse(){ Course cor1 = new Course("1","语文"); Course cor2 = new Course("2","数学"); courseList.add(cor1); courseList.add(cor2); Course[] cors = { new Course("3","英语"), new Course("4","体育") }; courseList.addAll(Arrays.asList(cors)); } //通过for循环显示备选课程 public void showCourseList(){ System.out.println("For循环:当前所有的备选课程有:"); for (Object c : courseList){ //或者 for(int i=0;i<courseList.size();i++) if(c instanceof Course){ Course course = (Course)c; System.out.println(course.id+":"+course.name); } } } //通过迭代器Iterator循环 public void showCourseList2(){ System.out.println("Iterator循环:当前所有的备选课程有:"); Iterator it = courseList.iterator(); while (it.hasNext()){ Course course = (Course)it.next(); System.out.println(course.id+":"+course.name); } } //通过集合的forEach方法lambda表达式 public void showCourseList3(){ System.out.println("forEach的lambda表达式:当前所有的备选课程有:"); courseList.forEach(o -> { Course course = (Course)o; System.out.println(course.id+":"+course.name); }); } //修改List的内容 public void courseModify(){ Course oc = new Course("4","编程"); courseList.set(3,oc);//修改index = 3的元素内容 System.out.println("第4个元素被修改成功!"); } public static void main(String[] args) { Test t = new Test(); t.initCourse(); t.showCourseList(); t.showCourseList2(); t.showCourseList3(); t.courseModify();; t.showCourseList3(); } }
查看全部 -
package qmh.demo1; import java.util.InputMismatchException; import java.util.Scanner; public class ChainTest { public static void main(String[] args) throws BookException{ ChainTest ct = new ChainTest(); // 创建图书数组 Book[] books = {new Book("数据结构",1),new Book("计算机网络",2),new Book("c++",3)}; Scanner sc = null; //输入对象 int order = -1;// 用户的选择 String bookName = null; //图书名称 int bookNum = 0; // 图书序号 boolean ifHaveBook = false; //是否存在书 Book chooseBook=null;// 用户选择的书 while(order!=1 && order!=2){ System.out.println("输入命令:1-按照名称查找图书; 2-按照序号查找图书"); try{ sc = new Scanner(System.in); order = sc.nextInt(); if(order!=1&&order!=2){ throw new BookException("请输入1或2"); } switch (order){ case 1: System.out.println("请输入图书名称"); sc = new Scanner(System.in); bookName = sc.nextLine(); for(Book k:books){ if(k.getName().equals(bookName)){ ifHaveBook = true; chooseBook = k; } } break; case 2: System.out.println("请输入图书序号"); sc = new Scanner(System.in); bookNum = sc.nextInt(); for(Book k:books) { if (k.getNumber() == bookNum) { ifHaveBook = true; chooseBook = k; } } break; } if(ifHaveBook){ System.out.println(chooseBook.name); }else{ order = -1; //开启下一次循环 throw new BookException("图书没有找到"); } }catch (InputMismatchException e){ System.out.println("请输入整数!"); }catch (BookException e){ e.getStackTrace();//打印异常 } } } }
查看全部 -
package com.kandy.class8; public class Book { private String name; private int id; public String getName() { return name; } public Book(int id,String name) { this.name = name; this.id = id; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } } package com.kandy.class8; public class BookNotFindExeption extends Exception{ public BookNotFindExeption(){} public BookNotFindExeption(String message) { super(message); } } package com.kandy.class8; import java.util.Scanner; public class InitialBook { //创建图书 Book[] books = { new Book(1, "语文"), new Book(2, "数学"), new Book(3, "英语"), }; //输入 Scanner input; //显示图书菜单,进入查找分类 public void showMenu() { try { System.out.println("借书请按编号或书名(1:按编号/2:按书名)"); System.out.println("编号\t\t 书名"); for (Book book : books) { System.out.println(" " + book.getId() + "\t\t《" + book.getName() + "》"); } System.out.print("请输入1或2,0退出:"); input = new Scanner(System.in); int num = input.nextInt(); switch (num) { case 0: System.exit(0); break; case 1: findForId(); break; case 2: findForName(); break; default: showMenu(); } } catch (Exception e) { System.out.println("输入有误,请根据提示输入。"); showMenu(); } finally { input.close(); } } //按编号查找 public void findForId() { try { System.out.print("请输入图书的编号,输入0返回上一级:"); input = new Scanner(System.in); int num = input.nextInt(); if (num == 0) showMenu(); boolean isFind = false; for (Book book : books) { if (book.getId() == num) { isFind = true; System.out.println("********找到图书:(编号:" + book.getId() + "书名:" + book.getName() + ")"); showMenu(); break; } } if (!isFind) { throw new BookNotFindExeption("未找到此编号的图书!"); } } catch (BookNotFindExeption e) { System.out.println(e.getMessage()); findForId(); } catch (Exception e) { System.out.println("输入有误,请根据提示输入。"); findForId(); } finally { input.close(); } } //按书名查找 public void findForName() { try { System.out.print("请输入图书的名称,输入0返回上一级:"); input = new Scanner(System.in); String name = input.nextLine(); if (name.equals("0")) showMenu(); boolean isFind = false; for (Book book : books) { if (book.getName().equals(name)) { isFind = true; System.out.println("*********找到图书:(编号:" + book.getId() + "书名:" + book.getName() + ")"); showMenu(); break; } } if (!isFind) { throw new BookNotFindExeption("未找到此名称的图书!"); } } catch (BookNotFindExeption e) { System.out.println(e.getMessage()); findForName(); } catch (Exception e) { System.out.println("输入有误,请根据提示输入。"); findForName(); } finally { input.close(); } } //程序入口 public static void main(String[] args) { InitialBook in = new InitialBook(); //显示图书列表 in.showMenu(); } }
查看全部 -
集合类:一种工具类,就像是容器,存储任意数量的具有共同属性的对象。
集合的作用:
在类的内部,对数据进行组织。
简单而快速的搜索大数量的条目。
可以在序列中快速插入或删除有关元素。
可以通过关键字快速查找到对应的唯一对象。
查看全部 -
异常类的父子关系
查看全部 -
try-catch-catch-----
查看全部 -
collection和Map
查看全部 -
用list储存class的时候初始化
test = new ArrayList();
储存进去的是object不再是原来的class
取出时记得转换类型
不可超过list长度
查看全部 -
package work1;
import java.util.Scanner;
public class BookManagerEasy {
private static Scanner sc = new Scanner(System.in);
//从控制台输入命令,用于输入命令和图书序号
private static int inputCommand(){
int command;
try{
command = sc.nextInt();
return command;
}catch(Exception e){
sc = new Scanner(System.in);//重新输入
return -1;
}
}
private static String getBookByName(String[] books)
throws Exception{
System.out.println("输入图书名称");
String name = sc.next();
for(int i = 0; i < books.length; i++){
if (name.equals(books[i])) {
return books[i];
}
}
//若无匹配,抛出”图书不存在异常“
throw new Exception("图书不存在!");
}
private static String getBookByNumber(String[] books)
throws Exception{
// TODO Auto-generated method stub
while(true){
System.out.println("请输入图书序号:");
try {
int index = inputCommand();
if (index == -1) {
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
}
//若不出现”数组下标越界异常“,则返回相应位置的图书
String book = books[index];
return book;
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
e.printStackTrace();
//输入的序号不存在(引发”数组下标越界异常“),则抛出”图书不存在异常“
Exception bookNotExists = new Exception("图书不存在!");
bookNotExists.initCause(e);
throw bookNotExists;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] books = { "C语言",
"数据结构",
"汇编语言",
"高数",
"大学语文",
"毛概"};
while(true){
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
String book;
try {
//取得整型命令
int command = inputCommand();
//根据不同的命令进行不同的操作
switch (command) {
case 1://根据图书名称选择图书
book = getBookByName(books);
System.out.println("book:"+ book);
break;
case 2://根据图书序号选择图书
book = getBookByNumber(books);
System.out.println("book:"+ book);
break;
case -1://返回值为-1,说明输入有错误
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
default://其他值的命令均认为是错误命令
System.out.println("命令输入错误!");
break;
}
} catch (Exception bne) {
bne.printStackTrace();
System.out.println(bne.getMessage());
// TODO: handle exception
continue;
}
}
}
}
查看全部 -
package work1;
import java.util.Scanner;
public class BookManagerEasy {
private static Scanner sc = new Scanner(System.in);
//从控制台输入命令,用于输入命令和图书序号
private static int inputCommand(){
int command;
try{
command = sc.nextInt();
return command;
}catch(Exception e){
sc = new Scanner(System.in);//重新输入
return -1;
}
}
private static String getBookByName(String[] books)
throws Exception{
System.out.println("输入图书名称");
String name = sc.next();
for(int i = 0; i < books.length; i++){
if (name.equals(books[i])) {
return books[i];
}
}
//若无匹配,抛出”图书不存在异常“
throw new Exception("图书不存在!");
}
private static String getBookByNumber(String[] books)
throws Exception{
// TODO Auto-generated method stub
while(true){
System.out.println("请输入图书序号:");
try {
int index = inputCommand();
if (index == -1) {
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
}
//若不出现”数组下标越界异常“,则返回相应位置的图书
String book = books[index];
return book;
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
e.printStackTrace();
//输入的序号不存在(引发”数组下标越界异常“),则抛出”图书不存在异常“
Exception bookNotExists = new Exception("图书不存在!");
bookNotExists.initCause(e);
throw bookNotExists;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] books = { "C语言",
"数据结构",
"汇编语言",
"高数",
"大学语文",
"毛概"};
while(true){
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
String book;
try {
//取得整型命令
int command = inputCommand();
//根据不同的命令进行不同的操作
switch (command) {
case 1://根据图书名称选择图书
book = getBookByName(books);
System.out.println("book:"+ book);
break;
case 2://根据图书序号选择图书
book = getBookByNumber(books);
System.out.println("book:"+ book);
break;
case -1://返回值为-1,说明输入有错误
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
default://其他值的命令均认为是错误命令
System.out.println("命令输入错误!");
break;
}
} catch (Exception bne) {
bne.printStackTrace();
System.out.println(bne.getMessage());
// TODO: handle exception
continue;
}
}
}
}
查看全部
举报