-
记录一下,按照自己的想法写的一些代码,留观日后查看,其他同学也可进行参照
查看全部 -
package com.imooc.practice; import java.util.InputMismatchException; import java.util.Scanner; /** * 要求: * 1、定义字符串数组保存图书信息 * 2、提示用户输入,分别按“书名”和“图书序号”查找图书 * 3、根据输入信息进行适当的异常处理 * a、如果输入类型错误,抛出“错误命令异常”,并提示重新输入 * b、如果书名不存在,抛出“图书不存在异常”,并提示重新输入 * c、如果图书序号超过字符串数组范围,抛出“图书不存在异常”,并提示重新输入 */ public class BorrowBooks { public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"}; public static void main(String[] args) { BorrowBooks borrowBooks = new BorrowBooks(); borrowBooks.test(); } /** * 调用方法 */ public void test() { try { System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书"); int input = new Scanner(System.in).nextInt(); if (input == 1) { System.out.println("输入图书名称:"); queryBookName(); } else if (input == 2) { System.out.println("输入图书序号:"); queryBookNum(); } } catch (InputMismatchException e) { System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); } } /** * 通过书名进行查询 */ public void queryBookName() { String bookname = new Scanner(System.in).next(); int booktype = 0; for (String book : books ) { if (bookname.equals(book)) { System.out.println("书名:" + bookname); booktype = 1; break; } } if (booktype == 0) { System.out.println("图书不存在!"); test(); } } /** * 通过序号进行查询 */ public void queryBookNum() { try { int booknum = new Scanner(System.in).nextInt(); for (int i = 0; i < books.length; i++) { if (i == booknum) { System.out.println("书名:" + books[i-1]); } } }catch (InputMismatchException e){ System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); }catch (ArrayIndexOutOfBoundsException e){ System.out.println("图书不存在!"); test(); } } }
查看全部 -
package com.imooc.practice; import java.util.InputMismatchException; import java.util.Scanner; /** * 要求: * 1、定义字符串数组保存图书信息 * 2、提示用户输入,分别按“书名”和“图书序号”查找图书 * 3、根据输入信息进行适当的异常处理 * a、如果输入类型错误,抛出“错误命令异常”,并提示重新输入 * b、如果书名不存在,抛出“图书不存在异常”,并提示重新输入 * c、如果图书序号超过字符串数组范围,抛出“图书不存在异常”,并提示重新输入 */ public class BorrowBooks { public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"}; public static void main(String[] args) { BorrowBooks borrowBooks = new BorrowBooks(); borrowBooks.test(); } /** * 调用方法 */ public void test() { try { System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书"); int input = new Scanner(System.in).nextInt(); if (input == 1) { System.out.println("输入图书名称:"); queryBookName(); } else if (input == 2) { System.out.println("输入图书序号:"); queryBookNum(); } } catch (InputMismatchException e) { System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); } } /** * 通过书名进行查询 */ public void queryBookName() { String bookname = new Scanner(System.in).next(); int booktype = 0; for (String book : books ) { if (bookname.equals(book)) { System.out.println("书名:" + bookname); booktype = 1; break; } } if (booktype == 0) { System.out.println("图书不存在!"); test(); } } /** * 通过序号进行查询 */ public void queryBookNum() { try { int booknum = new Scanner(System.in).nextInt(); for (int i = 0; i < books.length; i++) { if (i == booknum) { System.out.println("书名:" + books[i-1]); } } }catch (InputMismatchException e){ System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); }catch (ArrayIndexOutOfBoundsException e){ System.out.println("图书不存在!"); test(); } } }
查看全部 -
package com.imooc.practice; import java.util.InputMismatchException; import java.util.Scanner; public class BorrowBooks { public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"}; public static void main(String[] args) { BorrowBooks borrowBooks = new BorrowBooks(); borrowBooks.test(); } public void test() { try { System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书"); int input = new Scanner(System.in).nextInt(); if (input == 1) { System.out.println("输入图书名称:"); queryBookName(); } else if (input == 2) { System.out.println("输入图书序号:"); queryBookNum(); } } catch (InputMismatchException e) { System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); } } public void queryBookName() { String bookname = new Scanner(System.in).next(); int booktype = 0; for (String book : books ) { if (bookname.equals(book)) { System.out.println("书名:" + bookname); booktype = 1; break; } } if (booktype == 0) { System.out.println("图书不存在!"); test(); } } public void queryBookNum() { try { int booknum = new Scanner(System.in).nextInt(); for (int i = 0; i < books.length; i++) { if (i == booknum) { System.out.println("书名:" + books[i-1]); } } }catch (InputMismatchException e){ System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); }catch (ArrayIndexOutOfBoundsException e){ System.out.println("图书不存在!"); test(); } } }
查看全部 -
package com.imooc.practice; import java.util.InputMismatchException; import java.util.Scanner; public class BorrowBooks { public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"}; public static void main(String[] args) { BorrowBooks borrowBooks = new BorrowBooks(); borrowBooks.test(); } /** * 调用方法 */ public void test() { try { System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书"); int input = new Scanner(System.in).nextInt(); if (input == 1) { System.out.println("输入图书名称:"); queryBookName(); } else if (input == 2) { System.out.println("输入图书序号:"); queryBookNum(); } } catch (InputMismatchException e) { System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); } } /** * 通过书名进行查询 */ public void queryBookName() { String bookname = new Scanner(System.in).next(); int booktype = 0; for (String book : books ) { if (bookname.equals(book)) { System.out.println("书名:" + bookname); booktype = 1; break; } } if (booktype == 0) { System.out.println("图书不存在!"); test(); } } /** * 通过序号进行查询 */ public void queryBookNum() { try { int booknum = new Scanner(System.in).nextInt(); for (int i = 0; i < books.length; i++) { if (i == booknum) { System.out.println("书名:" + books[i-1]); } } }catch (InputMismatchException e){ System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); }catch (ArrayIndexOutOfBoundsException e){ System.out.println("图书不存在!"); test(); } } }
查看全部 -
package com.imooc.practice; import java.util.InputMismatchException; import java.util.Scanner; public class BorrowBooks { public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"}; public static void main(String[] args) { BorrowBooks borrowBooks = new BorrowBooks(); borrowBooks.test(); } /** * 调用方法 */ public void test() { try { System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书"); int input = new Scanner(System.in).nextInt(); if (input == 1) { System.out.println("输入图书名称:"); queryBookName(); } else if (input == 2) { System.out.println("输入图书序号:"); queryBookNum(); } } catch (InputMismatchException e) { System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); } } /** * 通过书名进行查询 */ public void queryBookName() { String bookname = new Scanner(System.in).next(); int booktype = 0; for (String book : books ) { if (bookname.equals(book)) { System.out.println("书名:" + bookname); booktype = 1; break; } } if (booktype == 0) { System.out.println("图书不存在!"); test(); } } /** * 通过序号进行查询 */ public void queryBookNum() { try { int booknum = new Scanner(System.in).nextInt(); for (int i = 0; i < books.length; i++) { if (i == booknum) { System.out.println("书名:" + books[i-1]); } } }catch (InputMismatchException e){ System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); }catch (ArrayIndexOutOfBoundsException e){ System.out.println("图书不存在!"); test(); } } }
查看全部 -
package com.imooc.practice; import java.util.InputMismatchException; import java.util.Scanner; /** * 要求: * 1、定义字符串数组保存图书信息 * 2、提示用户输入,分别按“书名”和“图书序号”查找图书 * 3、根据输入信息进行适当的异常处理 * a、如果输入类型错误,抛出“错误命令异常”,并提示重新输入 * b、如果书名不存在,抛出“图书不存在异常”,并提示重新输入 * c、如果图书序号超过字符串数组范围,抛出“图书不存在异常”,并提示重新输入 */ public class BorrowBooks { public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"}; public static void main(String[] args) { BorrowBooks borrowBooks = new BorrowBooks(); borrowBooks.test(); } /** * 调用方法 */ public void test() { try { System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书"); int input = new Scanner(System.in).nextInt(); if (input == 1) { System.out.println("输入图书名称:"); queryBookName(); } else if (input == 2) { System.out.println("输入图书序号:"); queryBookNum(); } } catch (InputMismatchException e) { System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); } } /** * 通过书名进行查询 */ public void queryBookName() { String bookname = new Scanner(System.in).next(); int booktype = 0; for (String book : books ) { if (bookname.equals(book)) { System.out.println("书名:" + bookname); booktype = 1; break; } } if (booktype == 0) { System.out.println("图书不存在!"); test(); } } /** * 通过序号进行查询 */ public void queryBookNum() { try { int booknum = new Scanner(System.in).nextInt(); for (int i = 0; i < books.length; i++) { if (i == booknum) { System.out.println("书名:" + books[i-1]); } } }catch (InputMismatchException e){ System.out.println("命令输入错误,请根据提示输入数字命令!"); test(); }catch (ArrayIndexOutOfBoundsException e){ System.out.println("图书不存在!"); test(); } } }
查看全部 -
记录一下,按照自己的想法写到的代码。其他同学也可进行参照
package com.imooc.practice;
import java.util.InputMismatchException;
import java.util.Scanner;
public class BorrowBooks {
public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"};
public static void main(String[] args) {
BorrowBooks borrowBooks = new BorrowBooks();
borrowBooks.test();
}
/**
* 调用方法
*/
public void test() {
try {
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书");
int input = new Scanner(System.in).nextInt();
if (input == 1) {
System.out.println("输入图书名称:");
queryBookName();
} else if (input == 2) {
System.out.println("输入图书序号:");
queryBookNum();
}
} catch (InputMismatchException e) {
System.out.println("命令输入错误,请根据提示输入数字命令!");
test();
}
}
/**
* 通过书名进行查询
*/
public void queryBookName() {
String bookname = new Scanner(System.in).next();
int booktype = 0;
for (String book : books
) {
if (bookname.equals(book)) {
System.out.println("书名:" + bookname);
booktype = 1;
break;
}
}
if (booktype == 0) {
System.out.println("图书不存在!");
test();
}
}
/**
* 通过序号进行查询
*/
public void queryBookNum() {
try {
int booknum = new Scanner(System.in).nextInt();
for (int i = 0; i < books.length; i++) {
if (i == booknum) {
System.out.println("书名:" + books[i-1]);
}
}
}catch (InputMismatchException e){
System.out.println("命令输入错误,请根据提示输入数字命令!");
test();
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("图书不存在!");
test();
}
}
}查看全部 -
记录一下,按照自己的想法写到的代码,留观日后需要。其他同学也可进行参照
package com.imooc.practice;
import java.util.InputMismatchException;
import java.util.Scanner;
public class BorrowBooks {
public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"};
public static void main(String[] args) {
BorrowBooks borrowBooks = new BorrowBooks();
borrowBooks.test();
}
/**
* 调用方法
*/
public void test() {
try {
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书");
int input = new Scanner(System.in).nextInt();
if (input == 1) {
System.out.println("输入图书名称:");
queryBookName();
} else if (input == 2) {
System.out.println("输入图书序号:");
queryBookNum();
}
} catch (InputMismatchException e) {
System.out.println("命令输入错误,请根据提示输入数字命令!");
test();
}
}
/**
* 通过书名进行查询
*/
public void queryBookName() {
String bookname = new Scanner(System.in).next();
int booktype = 0;
for (String book : books
) {
if (bookname.equals(book)) {
System.out.println("书名:" + bookname);
booktype = 1;
break;
}
}
if (booktype == 0) {
System.out.println("图书不存在!");
test();
}
}
/**
* 通过序号进行查询
*/
public void queryBookNum() {
try {
int booknum = new Scanner(System.in).nextInt();
for (int i = 0; i < books.length; i++) {
if (i == booknum) {
System.out.println("书名:" + books[i-1]);
}
}
}catch (InputMismatchException e){
System.out.println("命令输入错误,请根据提示输入数字命令!");
test();
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("图书不存在!");
test();
}
}
}查看全部 -
记录一下,按照自己的想法写到的代码,留观日后需要。其他同学也可进行参照
package com.imooc.practice;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* 要求:
* 1、定义字符串数组保存图书信息
* 2、提示用户输入,分别按“书名”和“图书序号”查找图书
* 3、根据输入信息进行适当的异常处理
* a、如果输入类型错误,抛出“错误命令异常”,并提示重新输入
* b、如果书名不存在,抛出“图书不存在异常”,并提示重新输入
* c、如果图书序号超过字符串数组范围,抛出“图书不存在异常”,并提示重新输入
*/
public class BorrowBooks {
public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"};
public static void main(String[] args) {
BorrowBooks borrowBooks = new BorrowBooks();
borrowBooks.test();
}
/**
* 调用方法
*/
public void test() {
try {
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书");
int input = new Scanner(System.in).nextInt();
if (input == 1) {
System.out.println("输入图书名称:");
queryBookName();
} else if (input == 2) {
System.out.println("输入图书序号:");
queryBookNum();
}
} catch (InputMismatchException e) {
System.out.println("命令输入错误,请根据提示输入数字命令!");
test();
}
}
/**
* 通过书名进行查询
*/
public void queryBookName() {
String bookname = new Scanner(System.in).next();
int booktype = 0;
for (String book : books
) {
if (bookname.equals(book)) {
System.out.println("书名:" + bookname);
booktype = 1;
break;
}
}
if (booktype == 0) {
System.out.println("图书不存在!");
test();
}
}
/**
* 通过序号进行查询
*/
public void queryBookNum() {
try {
int booknum = new Scanner(System.in).nextInt();
for (int i = 0; i < books.length; i++) {
if (i == booknum) {
System.out.println("书名:" + books[i-1]);
}
}
}catch (InputMismatchException e){
System.out.println("命令输入错误,请根据提示输入数字命令!");
test();
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("图书不存在!");
test();
}
}
}查看全部 -
记录一下,按照自己的想法写到的代码,留观日后需要。其他同学也可进行参照
package com.imooc.practice;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* 要求:
* 1、定义字符串数组保存图书信息
* 2、提示用户输入,分别按“书名”和“图书序号”查找图书
* 3、根据输入信息进行适当的异常处理
* a、如果输入类型错误,抛出“错误命令异常”,并提示重新输入
* b、如果书名不存在,抛出“图书不存在异常”,并提示重新输入
* c、如果图书序号超过字符串数组范围,抛出“图书不存在异常”,并提示重新输入
*/
public class BorrowBooks {
public static String[] books = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"};
public static void main(String[] args) {
BorrowBooks borrowBooks = new BorrowBooks();
borrowBooks.test();
}
/**
* 调用方法
*/
public void test() {
try {
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查询图书");
int input = new Scanner(System.in).nextInt();
if (input == 1) {
System.out.println("输入图书名称:");
queryBookName();
} else if (input == 2) {
System.out.println("输入图书序号:");
queryBookNum();
}
} catch (InputMismatchException e) {
System.out.println("命令输入错误,请根据提示输入数字命令!");
test();
}
}
/**
* 通过书名进行查询
*/
public void queryBookName() {
String bookname = new Scanner(System.in).next();
int booktype = 0;
for (String book : books
) {
if (bookname.equals(book)) {
System.out.println("书名:" + bookname);
booktype = 1;
break;
}
}
if (booktype == 0) {
System.out.println("图书不存在!");
test();
}
}
/**
* 通过序号进行查询
*/
public void queryBookNum() {
try {
int booknum = new Scanner(System.in).nextInt();
for (int i = 0; i < books.length; i++) {
if (i == booknum) {
System.out.println("书名:" + books[i-1]);
}
}
}catch (InputMismatchException e){
System.out.println("命令输入错误,请根据提示输入数字命令!");
test();
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("图书不存在!");
test();
}
}
}查看全部 -
package collection; import com.sun.xml.internal.bind.v2.model.core.ID; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set; /** * @author ChristinaXinny * @create 2021-09-30 1:02 下午 */ public class MapTest { public Map<String, Student>stuMap; public MapTest() { this.stuMap = new HashMap<>(); } //input public void testPut(){ Scanner in = new Scanner(System.in); int i=0; while (i<3){ System.out.println("input id:"); String id = in.next(); Student oldStu = stuMap.get(id); if (oldStu == null){ System.out.println("input name:"); String name = in.next(); Student newStu = new Student(id, name); stuMap.put(id,newStu); System.out.println("input success"); i++; } else { System.out.println("already have this student"); continue; } } } //删除 public void testRemove(){ Scanner in = new Scanner(System.in); while (true){ System.out.println("input remove id :"); String id = in.next(); Student st = stuMap.get(id); if (st == null){ System.out.println("id not exit"); continue; } stuMap.remove(id); System.out.println("success remove "+st.name); break; } } //遍历 public void testEnterSet(){ Set<Map.Entry<String, Student>> enters = stuMap.entrySet(); for (Map.Entry<String, Student> entry : enters){ System.out.println(entry.getKey()); System.out.println(entry.getValue().name); } } // 遍历 public void testKaySet(){ Set<String> keSet = stuMap.keySet(); System.out.println("have:"+stuMap.size()); for (String stuId : keSet){ Student st = stuMap.get(stuId); if (st!=null){ System.out.println(st.id+":"+ st.name); } } } public void testModify(){ System.out.println("modify id:"); Scanner in = new Scanner(System.in); while (true){ String stuId = in.next(); Student stu = stuMap.get(stuId); if(stu == null){ System.out.println("id not exit"); continue; } System.out.println("id:"+stuId +" name:"+stu.name); System.out.println("re-enter name"); String stuName = in.next(); Student newStu = new Student(stuId, stuName); stuMap.put(stuId, newStu ); System.out.println("modify success"); break; } } public static void main(String[] args) { MapTest mapTest = new MapTest(); mapTest.testPut(); mapTest.testKaySet(); mapTest.testRemove(); mapTest.testEnterSet(); mapTest.testModify(); mapTest.testKaySet(); } }
查看全部 -
package collection; import sun.applet.Main; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; /** * @author ChristinaXinny * @create 2021-09-30 12:37 下午 */ public class SetTest { public List<Course> coursesToSelect; public SetTest() { this.coursesToSelect = new ArrayList<Course>(); } //添加课程 public void testInsert() { //创建课程对象 Course c1 = new Course("1", "C++"); coursesToSelect.add(c1); Course temp = (Course) coursesToSelect.get(0); //System.out.println("add" + temp.id + ":" + temp.name); Course c2 = new Course("2", "Python"); coursesToSelect.add(0, c2); Course temp2 = (Course) coursesToSelect.get(0); //System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); Course[] course = {new Course("3", "Java"), new Course("4", "Vue")}; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3); //System.out.println("add two course:" + temp3.id + ":" + temp3.name + ":" + temp4.id + ":" + temp4.name); Course[] course2 = {new Course("5", "高数"), new Course("6", "大英")}; coursesToSelect.addAll(2, Arrays.asList(course2)); Course temp5 = (Course) coursesToSelect.get(2); Course temp6 = (Course) coursesToSelect.get(3); //System.out.println("add two course:" + temp5.id + ":" + temp5.name + ":" + temp6.id + ":" + temp6.name); } //通过for each方法访问集合元素 public void testForEach() { System.out.println("now have (for each):"); for (Object obj : coursesToSelect) { Course cr = (Course) obj; System.out.println("course: " + cr.id + ":" + cr.name); } } public static void main(String[] args) { SetTest setTest = new SetTest(); setTest.testInsert(); setTest.testForEach(); Student stu =new Student("1","xx"); System.out.println(stu); Scanner in = new Scanner(System.in); for (int i=0; i<3; i++){ System.out.println("input id :"); String id = in.next(); for (Course c : setTest.coursesToSelect){ if (c.id.equals(id)){ stu.courses.add(c); stu.courses.add(c); } } } setTest.testForEachSet(stu); } public void testForEachSet(Student stu ){ for (Course c : stu.courses){ System.out.println("select id:"+c.id + " name:"+c.name); } } }
查看全部 -
// 使用format()方法将日期转换为指定格式的文本
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 创建Date对象,表示当前时间
Date now = new Date();
// 调用format()方法,将日期转换为字符串并输出
System.out.println(sdf1.format(now));
System.out.println(sdf2.format(now));
System.out.println(sdf3.format(now));
// 使用parse()方法将文本转换为日期
String d = "2014-6-1 21:05:36";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 调用parse()方法,将字符串转换为日期
Date date = sdf.parse(d);
System.out.println(date);// 使用format()方法将日期转换为指定格式的文本 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm"); SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 创建Date对象,表示当前时间 Date now = new Date(); // 调用format()方法,将日期转换为字符串并输出 System.out.println(sdf1.format(now)); System.out.println(sdf2.format(now)); System.out.println(sdf3.format(now)); // 使用parse()方法将文本转换为日期 String d = "2014-6-1 21:05:36"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 调用parse()方法,将字符串转换为日期 Date date = sdf.parse(d); System.out.println(date);
查看全部 -
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class HelloWorld { public static void main(String[] args) throws ParseException { // 使用format()方法将日期转换为指定格式的文本 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm"); SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 创建Date对象,表示当前时间 Date now = new Date(); // 调用format()方法,将日期转换为字符串并输出 System.out.println(sdf1.format(now)); System.out.println(sdf2.format(now)); System.out.println(sdf3.format(now)); // 使用parse()方法将文本转换为日期 String d = "2014-6-1 21:05:36"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 调用parse()方法,将字符串转换为日期 Date date = sdf.parse(d); System.out.println(date); }}
查看全部 -
泛型
1、规定List定义的容器只能存储某种类型的元素。
不用泛型的话是什么类型的元素都可以插入,因为插入集合是忽略其类型,把它看成为Object类型,取出时需要强行转换对应的类型。
2、如何声明
List<Course> courseToSelect;
在构造方法中(实例化的时候也可以不在构造方法,直接实例就可以)
this.courseToSelect=new ArrayList<Course>();
3、添加
泛型集合中,不能添加泛型规定的类型以及其子类型以外的对象,否则会报错。
4、遍历
用foreach存储变量是泛型的类型。
for(Course a:courseToSelect),不用Object取出再强转,因为已经规定容器里装的都是Course类型。
查看全部 -
package collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; /** * @author ChristinaXinny * @create 2021-09-29 11:48 上午 */ public class ListTest { //用于存放备选课程的list public List coursesToSelect; public ListTest() { this.coursesToSelect = new ArrayList(); } //添加课程 public void testInsert() { //创建课程对象 Course c1 = new Course("1", "C++"); coursesToSelect.add(c1); Course temp = (Course) coursesToSelect.get(0); System.out.println("add" + temp.id + ":" + temp.name); Course c2 = new Course("2", "Python"); coursesToSelect.add(0, c2); Course temp2 = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); Course[] course = {new Course("3", "Java"), new Course("4", "Vue")}; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3); System.out.println("add two course:" + temp3.id + ":" + temp3.name + ":" + temp4.id + ":" + temp4.name); Course[] course2 = {new Course("5", "高数"), new Course("6", "大英")}; coursesToSelect.addAll(2, Arrays.asList(course2)); Course temp5 = (Course) coursesToSelect.get(2); Course temp6 = (Course) coursesToSelect.get(3); System.out.println("add two course:" + temp5.id + ":" + temp5.name + ":" + temp6.id + ":" + temp6.name); } //普通通过index遍历 public void testGet() { int size = coursesToSelect.size(); System.out.println("now have:"); for (int i = 0; i < size; i++) { Course cr = (Course) coursesToSelect.get(i); System.out.println("course:" + cr.id + ":" + cr.name); } } //通过迭代器来遍历list public void testIterator() { Iterator it = coursesToSelect.iterator(); System.out.println("now have:"); while (it.hasNext()) { Course cr = (Course) it.next(); System.out.println("course:" + cr.id + ":" + cr.name); } } //通过for each方法访问集合元素 public void testForEach() { System.out.println("now have (for each):"); for (Object obj : coursesToSelect) { Course cr = (Course) obj; System.out.println("course: " + cr.id + ":" + cr.name); } } //修改list中的元素 public void testModify(){ coursesToSelect.set(4,new Course("7","编译原理")); } //删除list中的元素 public void testDelete(){ //方法1 /**Course cr=(Course)coursesToSelect.get(4); System.out.println("我是课程:"+cr.id+";"+cr.name+",我即将被删除"); coursesToSelect.remove(cr);*/ //方法2 /**System.out.println("即将删除4位置上的课程"); coursesToSelect.remove(4); System.out.println("成功删除课程!");*/ //方法3 System.out.println("delete index=4 and index = 5 course"); Course[] courses={(Course)coursesToSelect.get(4),(Course)coursesToSelect.get(5)}; coursesToSelect.removeAll(Arrays.asList(courses));//将数组转换为集合 System.out.println("delete success!"); testForEach(); } public static void main(String[] args) { ListTest it=new ListTest(); it.testInsert(); it.testGet(); it.testIterator(); it.testForEach(); it.testModify(); it.testForEach(); it.testDelete(); } }
查看全部 -
package collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; /** * @author Xinny * @create 2021-09-29 11:48 上午 */ public class ListTest { //用于存放备选课程的list public List coursesToSelect; public ListTest() { this.coursesToSelect = new ArrayList(); } //添加课程 public void testInsert() { //创建课程对象 Course c1 = new Course("1", "C++"); coursesToSelect.add(c1); Course temp = (Course) coursesToSelect.get(0); System.out.println("add" + temp.id + ":" + temp.name); Course c2 = new Course("2", "Python"); coursesToSelect.add(0, c2); Course temp2 = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); Course[] course = { new Course("3", "Java"), new Course("4", "Vue")}; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3); System.out.println("add two course:" + temp3.id + ":" + temp3.name + ":" + temp4.id + ":" + temp4.name); Course[] course2 = {new Course("5", "高数"), new Course("6", "大英")}; coursesToSelect.addAll(2, Arrays.asList(course2)); Course temp5 = (Course) coursesToSelect.get(2); Course temp6 = (Course) coursesToSelect.get(3); System.out.println("add two course:" + temp5.id + ":" + temp5.name + ":" + temp6.id + ":" + temp6.name); } public void testGet() { int size = coursesToSelect.size(); System.out.println("now have:"); for (int i = 0; i < size; i++) { Course cr = (Course) coursesToSelect.get(i); System.out.println("course:" + cr.id + ":" + cr.name); } } //通过迭代器来遍历list public void testIterator() { Iterator it = coursesToSelect.iterator(); System.out.println("now have:"); while (it.hasNext()) { Course cr = (Course) it.next(); System.out.println("course:" + cr.id + ":" + cr.name); } } //通过for each方法访问集合元素 public void testForEach() { System.out.println("now have (for each):"); for (Object obj : coursesToSelect) { Course cr = (Course) obj; System.out.println("course: " + cr.id + ":" + cr.name); } } //修改list中的元素 public void testModify(){ coursesToSelect.set(4,new Course("7","编译原理")); } //删除list中的元素 public void testDelete(){ //方法1 /**Course cr=(Course)coursesToSelect.get(4); System.out.println("我是课程:"+cr.id+";"+cr.name+",我即将被删除"); coursesToSelect.remove(cr);*/ //方法2 /**System.out.println("即将删除4位置上的课程"); coursesToSelect.remove(4); System.out.println("成功删除课程!");*/ //方法3 System.out.println("delete index=4 and index = 5 course"); Course[] courses={(Course)coursesToSelect.get(4),(Course)coursesToSelect.get(5)}; coursesToSelect.removeAll(Arrays.asList(courses));//将数组转换为集合 System.out.println("delete success!"); testForEach(); } public static void main(String[] args) { ListTest it=new ListTest(); it.testInsert(); it.testGet(); it.testIterator(); it.testForEach(); it.testModify(); it.testForEach(); it.testDelete(); } }
查看全部 -
package collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; /** * @author Xinny * @create 2021-09-29 11:48 上午 */ public class ListTest { //用于存放备选课程的list public List coursesToSelect; public ListTest() { this.coursesToSelect = new ArrayList(); } //添加课程 public void testInsert() { //创建课程对象 Course c1 = new Course("1", "C++"); coursesToSelect.add(c1); Course temp = (Course) coursesToSelect.get(0); System.out.println("add" + temp.id + ":" + temp.name); Course c2 = new Course("2", "Python"); coursesToSelect.add(0, c2); Course temp2 = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp2.id + ":" + temp2.name); Course[] course = { new Course("3", "Java"), new Course("4", "Vue")}; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3); System.out.println("add two course:" + temp3.id + ";" + temp3.name + ";" + temp4.id + ";" + temp4.name); Course[] course2 = {new Course("5", "高数"), new Course("6", "大英")}; coursesToSelect.addAll(2, Arrays.asList(course2)); Course temp5 = (Course) coursesToSelect.get(2); Course temp6 = (Course) coursesToSelect.get(3); System.out.println("add two course:" + temp5.id + ";" + temp5.name + ";" + temp6.id + ";" + temp6.name); } public void testGet() { int size = coursesToSelect.size(); System.out.println("now have:"); for (int i = 0; i < size; i++) { Course cr = (Course) coursesToSelect.get(i); System.out.println("course:" + cr.id + ":" + cr.name); } } //通过迭代器来遍历list public void testIterator() { Iterator it = coursesToSelect.iterator(); System.out.println("now have:"); while (it.hasNext()) { Course cr = (Course) it.next(); System.out.println("course:" + cr.id + ":" + cr.name); } } //通过for each方法访问集合元素 public void testForEach() { System.out.println("now have (for each):"); for (Object obj : coursesToSelect) { Course cr = (Course) obj; System.out.println("course: " + cr.id + ";" + cr.name); } } //修改list中的元素 public void testModify(){ coursesToSelect.set(4,new Course("7","编译原理")); } //删除list中的元素 public void testDelete(){ //方法1 /**Course cr=(Course)coursesToSelect.get(4); System.out.println("我是课程:"+cr.id+";"+cr.name+",我即将被删除"); coursesToSelect.remove(cr);*/ //方法2 /**System.out.println("即将删除4位置上的课程"); coursesToSelect.remove(4); System.out.println("成功删除课程!");*/ //方法3 System.out.println("delete index=4 and index = 5 course"); Course[] courses={(Course)coursesToSelect.get(4),(Course)coursesToSelect.get(5)}; coursesToSelect.removeAll(Arrays.asList(courses));//将数组转换为集合 System.out.println("delete success!"); testForEach(); } public static void main(String[] args) { ListTest it=new ListTest(); it.testInsert(); it.testGet(); it.testIterator(); it.testForEach(); it.testModify(); it.testForEach(); it.testDelete(); } }
查看全部 -
package collection; import java.util.ArrayList; import java.util.List; /** * @author Xinny * @create 2021-09-29 11:48 上午 */ public class ListTest { //用于存放备选课程的list public List coursesToSelect; public ListTest(){ this.coursesToSelect = new ArrayList(); } //添加课程 public void addC(){ //创建课程对象 Course c1 = new Course("1","C++"); coursesToSelect.add(c1); Course temp = (Course) coursesToSelect.get(0); System.out.println("add"+temp.id+":"+temp.name); Course c2 = new Course("2","Python"); coursesToSelect.add(0,c2); Course temp2 = (Course)coursesToSelect.get(0); System.out.println("添加了课程:"+temp2.id+":"+temp2.name); } public static void main(String[] args) { ListTest lt = new ListTest(); lt.addC(); } }
查看全部 -
Throwable 【所有异常都继承于Throwable类】
Error 错误,一般编程人员不太接触。会使程序崩溃
---->VirtualMachineError 虚拟机错误
---->ThreadDeath 线程死锁
Exception 编码、环境、用户操作输入问题
---->RuntimeException 【非检查异常(运行时异常】由java虚拟机自动捕获
---->NullPointer【空指针】
---->ArrayIndexOutofBounds【越界】
---->ClassCast【错误类型转换】
---->Arithmetic【算数异常】
---->....
---->CheckException【检查异常(需要手动添加捕获和处理语句】文件异常IO
---->IOException【文件异常】
---->SQLException【SQL连接异常】
---->....
查看全部 -
try{
//一些会抛出异常的方法
}catch (Exception e){
//处理该异常的代码块
}catch(Exception e){
//处理该异常的代码块
}
从子类到父类
Scanner input = new Scanner(System.in); try{ System.out.println("请输入第一个数:"); int one = input.nextInt(); System.out.println("请输入第二个数:"); int two = input.nextInt(); System.out.println("两数相除的结果为:"+one/two); }catch(InputMismatchException e){ System.out.println("你应该输入整数"); }catch(ArithmeticException e){ System.out.println("除数不能为0"); }catch(Exception e){ System.out.println("我是不知名异常"); }finally{ //最终代码 }
查看全部 -
2
查看全部 -
1
查看全部 -
笔记
查看全部
举报