Java實現(xiàn)簡易學(xué)籍管理系統(tǒng)
本文實例為大家分享了Java實現(xiàn)簡易學(xué)籍管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
1、我們先來講講什么是泛型化編程
package 學(xué)生管理系統(tǒng); ? import java.util.ArrayList; import java.util.List; ? /* ?* 進行泛型化編程 ?* @autor:pcb ?* */ public class TestGeneric { ?? ?//進行泛型化編程,規(guī)定類型。 ?? ?public List <CourseClass> courses; ?? ?public TestGeneric() { ?? ??? ?this.courses = new ArrayList<CourseClass>(); ?? ?} ?? ?public void testAdd() { ?? ??? ?CourseClass cr1 = new CourseClass("1","大學(xué)語文"); ?? ??? ?courses.add(cr1); ?? ??? ?//泛型集合中不能添加規(guī)定意外的對象 ?? ??? ?//courses.add("能否添加一些奇怪的東西"); ?? ??? ?CourseClass cr2 = new CourseClass("2","Java基礎(chǔ)"); ?? ??? ?courses.add(cr2); ?? ?} ?? ?public void testForEach() { ?? ??? ?//進行泛型之后,可直接利用它規(guī)定的類型及其子類型直接進行訪問 ?? ??? ?for(CourseClass cr:courses) { ?? ??? ??? ?System.out.println(cr.id+":"+cr.name); ?? ??? ?} ?? ?} ?? ?//泛型集合可以添加泛型的子類型的對象實例 ?? ?public void testChild() { ?? ??? ?ChildCourse ccr = new ChildCourse(); ?? ??? ?ccr.id = "3"; ?? ??? ?ccr.name = "我是子類的課程實例對象"; ?? ??? ?courses.add(ccr); ?? ?} ?? ?//泛型必須使用包裝類型進行創(chuàng)建,例如:int和integer ?? ?public void testBaic() { ?? ??? ?List<Integer> list = new ArrayList<Integer>(); ?? ??? ?list.add(1); ?? ??? ?System.out.println("基本類型必須使用包裝類作為泛型!"+list.get(0)); ?? ?} ?? ?public static void main(String[] args) { ?? ??? ?TestGeneric tg = new TestGeneric(); ?? ??? ?tg.testAdd(); ?? ??? ?tg.testForEach(); ?? ??? ?tg.testChild(); ?? ??? ?tg.testForEach(); ?? ??? ?tg.testBaic(); ?? ??? ?//tg.testForEach(); ?? ?} }
2、我們開始進行管理系統(tǒng)的創(chuàng)建,創(chuàng)建一個學(xué)生類和一個課程類
package 學(xué)生管理系統(tǒng); ? import java.util.HashSet; import java.util.Set; public class StudentsClass implements Comparable <StudentsClass> { ?? ?public String id; ?? ?public String name; ?? ? ?? ?public Set<CourseClass> courses; ?? ? ?? ?public StudentsClass(String id,String name){ ?? ??? ?this.id=id; ?? ??? ?this.name=name; ?? ??? ?this.courses=new HashSet<CourseClass>(); ?? ?} ? ?? ?@Override ?? ?public int hashCode() { ?? ??? ?final int prime = 31; ?? ??? ?int result = 1; ?? ??? ?result = prime * result + ((name == null) ? 0 : name.hashCode()); ?? ??? ?return result; ?? ?} ? ?? ?@Override ?? ?public boolean equals(Object obj) { ?? ??? ?if (this == obj) ?? ??? ??? ?return true; ?? ??? ?if (obj == null) ?? ??? ??? ?return false; ?? ??? ?if (!(obj instanceof StudentsClass)) ?? ??? ??? ?return false; ?? ??? ?StudentsClass other = (StudentsClass) obj; ?? ??? ?if (name == null) { ?? ??? ??? ?if (other.name != null) ?? ??? ??? ??? ?return false; ?? ??? ?} else if (!name.equals(other.name)) ?? ??? ??? ?return false; ?? ??? ?return true; ?? ?} ?? ?public int compareTo(StudentsClass o) { ?? ??? ?return this.id.compareTo(o.id); ?? ?} } package 學(xué)生管理系統(tǒng); ? public class CourseClass { ?? ?public String id; ?? ?public String name; ?? ?public CourseClass(String id,String name){ ?? ??? ?this.id=id; ?? ??? ?this.name=name; ?? ?} ?? ?public CourseClass() { ?? ??? ? ?? ?} ?? ? ?? ?@Override ?? ?public int hashCode() { ?? ??? ?final int prime = 31; ?? ??? ?int result = 1; ?? ??? ?result = prime * result + ((name == null) ? 0 : name.hashCode()); ?? ??? ?return result; ?? ?} ?? ?@Override ?? ?public boolean equals(Object obj) { ?? ??? ?if (this == obj) ?? ??? ??? ?return true; ?? ??? ?if (obj == null) ?? ??? ??? ?return false; ?? ??? ?if (!(obj instanceof CourseClass)) ?? ??? ??? ?return false; ?? ??? ?CourseClass other = (CourseClass) obj; ?? ??? ?if (name == null) { ?? ??? ??? ?if (other.name != null) ?? ??? ??? ??? ?return false; ?? ??? ?} else if (!name.equals(other.name)) ?? ??? ??? ?return false; ?? ??? ?return true; ?? ?} }
3、利用List進行增加課程,刪除課程,修改課程等的操作
package 學(xué)生管理系統(tǒng); import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; ? public class ListTest { ?? ?//創(chuàng)建 public List coursesToSelect; ?? ?//構(gòu)造器 ?? ?public ListTest(){ ?? ??? ? this.coursesToSelect = new ArrayList();? ?? ?} ?? ? ?? ?public void testAdd(){ ?? ??? ?//加課 ?? ??? ? ?CourseClass cr1 = new CourseClass("1","數(shù)據(jù)結(jié)構(gòu)"); ?? ??? ? ?coursesToSelect.add(cr1);? ?? ??? ? ?CourseClass temp = (CourseClass)coursesToSelect.get(0); ?? ??? ? ?System.out.println("添加了課程:"+temp.id+":"+temp.name); ?? ??? ? ?CourseClass cr2 = new CourseClass("2","c語言"); ?? ??? ? ?coursesToSelect.add(0, cr2); ?? ??? ? ?CourseClass temp2 = (CourseClass)coursesToSelect.get(0); ?? ??? ? ?System.out.println("添加了課程:"+temp2.id+":"+temp2.name); ?? ??? ? ?CourseClass[] Course={new CourseClass("3","離散數(shù)學(xué)"),new CourseClass("4","匯編語言")}; ?? ??? ? ?coursesToSelect.addAll(Arrays.asList(Course)); ?? ??? ? ?CourseClass temp3 = (CourseClass)coursesToSelect.get(2); ?? ??? ? ?CourseClass temp4 = (CourseClass)coursesToSelect.get(3); ?? ??? ? ?System.out.println("添加了兩門課程:"+temp3.id+":"+temp3.name+";"+temp4.id+":"+temp4.name); ?? ??? ? ?CourseClass[] Course1= {new CourseClass("5","高等數(shù)學(xué)"),new CourseClass("6","大學(xué)英語")}; ?? ??? ? ?coursesToSelect.addAll(2, Arrays.asList(Course1)); ?? ??? ? ?CourseClass temp5 = (CourseClass)coursesToSelect.get(2); ?? ??? ? ?CourseClass temp6 = (CourseClass)coursesToSelect.get(3); ?? ??? ? ?System.out.println("添加了兩門課程:"+temp5.id+":"+temp5.name+";"+temp6.id+":"+temp6.name); ? ?? ?} ?? ??? ?public void test() { ?? ??? ??? ?int size = coursesToSelect.size(); ?? ??? ??? ?for(int i=0;i<size;i++) { ?? ??? ??? ??? ?//進行強制類型轉(zhuǎn)化 ?? ??? ??? ??? ?CourseClass cr = (CourseClass)coursesToSelect.get(i); ?? ??? ??? ??? ?System.out.println("課程"+i+":"+cr.id+":"+cr.name); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?//迭代器輸入方法 ?? ??? ?public void testIterator() { ?? ??? ??? ?Iterator it = coursesToSelect.iterator(); ?? ??? ??? ?System.out.println("可選擇的課程(通過迭代器訪問):"); ?? ??? ??? ?while(it.hasNext()) { ?? ??? ??? ??? ?CourseClass cr ?= (CourseClass) it.next(); ?? ??? ??? ??? ?System.out.println("課程"+":"+cr.id+":"+cr.name); ?? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ?} ?? ??? ?//利用foreach語句進行遍歷 ?? ??? ?public void testForEach() { ?? ??? ??? ?System.out.println("可選擇的課程(通過FOREACH訪問):"); ?? ??? ??? ?for(Object obj:coursesToSelect) { ?? ??? ??? ??? ?CourseClass cr ?= (CourseClass) obj; ?? ??? ??? ??? ?System.out.println("課程"+":"+cr.id+":"+cr.name); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?//進行修改操作 ?? ??? ?public void testModify() { ?? ??? ??? ?coursesToSelect.set(4, new CourseClass("7","毛概")); ?? ??? ?} ?? ??? ?//利用對象進行刪除操作 ?? ??? ?public void testRmove1() { ?? ??? ??? ?System.out.println("通過對象進行刪除"); ?? ??? ??? ?CourseClass cr = (CourseClass) coursesToSelect.get(4); ?? ??? ??? ?System.out.println("我是課程:"+cr.id+":"+cr.name); ?? ??? ??? ?coursesToSelect.remove(cr); ?? ??? ??? ?System.out.println("成功刪除課程??!"); ?? ??? ??? ?testForEach(); ?? ??? ?} ?? ??? ?//利用索引值進行刪除操作 ?? ??? ?public void testRmove2() { //?? ??? ??? ?CourseClass cr = (CourseClass) coursesToSelect.get(4); //?? ??? ??? ?System.out.println("我是課程:"+cr.id+":"+cr.name); ?? ??? ??? ?System.out.println("通過索引值進行刪除"); ?? ??? ??? ?coursesToSelect.remove(4); ?? ??? ??? ?System.out.println("成功刪除課程??!"); ?? ??? ??? ?testForEach(); ?? ??? ?} ?? ??? ?//進行刪除,刪除多位置的方法操作 ?? ??? ?public void testRmove3() { ?? ??? ??? ?System.out.println("刪除4位置和5位置的課程"); ?? ??? ??? ?CourseClass [] Course={(CourseClass) coursesToSelect.get(4),(CourseClass) coursesToSelect.get(5)}; ?? ??? ??? ?//將Course數(shù)組轉(zhuǎn)化成一個集合傳遞進去 ?? ??? ??? ?coursesToSelect.removeAll(Arrays.asList(Course)); ?? ??? ??? ?System.out.println("成功刪除課程??!"); ?? ??? ??? ?testForEach(); ?? ??? ??? ?} ?? ?public static void main(String[] args) { ?? ??? ?ListTest it = new ListTest(); ?? ??? ?it.testAdd(); ?? ??? ?it.test(); ?? ??? ?//it.testIterator(); ?? ??? ?//it.testForEach(); ?? ??? ?//it.testModify(); ?? ??? ?//it.testForEach(); ?? ??? ?//it.testRmove2(); ?? ??? ?it.testRmove3(); ?? ?} }
4、利用set集合進行刪除,添加,查詢操作
package 學(xué)生管理系統(tǒng); ? import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; ? public class SetTest { ?? ?//創(chuàng)建 ?? ?public List <CourseClass> coursesToSelect; ?? ?//向屏幕讀取 ?? ?private Scanner console; ?? ? ?? ?public StudentsClass student; ?? ?//進行構(gòu)造 ?? ?public SetTest() { ?? ??? ?coursesToSelect = new ArrayList<CourseClass>(); ?? ??? ?console = new Scanner(System.in); ?? ?} ?? ? ?? ?public void testAdd(){ ?? ??? ?//加課 ?? ??? ? ?CourseClass cr1 = new CourseClass("1","數(shù)據(jù)結(jié)構(gòu)"); ?? ??? ? ?coursesToSelect.add(cr1);? ?? ??? ? ?CourseClass temp = (CourseClass)coursesToSelect.get(0); ?? ??? ? ?//System.out.println("添加了課程:"+temp.id+":"+temp.name); ?? ??? ? ?CourseClass cr2 = new CourseClass("2","c語言"); ?? ??? ? ?coursesToSelect.add(0, cr2); ?? ??? ? ?CourseClass temp2 = (CourseClass)coursesToSelect.get(0); ?? ??? ? ?//System.out.println("添加了課程:"+temp2.id+":"+temp2.name); ?? ??? ? ?CourseClass[] Course={new CourseClass("3","離散數(shù)學(xué)"),new CourseClass("4","匯編語言")}; ?? ??? ? ?coursesToSelect.addAll(Arrays.asList(Course)); ?? ??? ? ?CourseClass temp3 = (CourseClass)coursesToSelect.get(2); ?? ??? ? ?CourseClass temp4 = (CourseClass)coursesToSelect.get(3); ?? ??? ? ?//System.out.println("添加了兩門課程:"+temp3.id+":"+temp3.name+";"+temp4.id+":"+temp4.name); ?? ??? ? ?CourseClass[] Course1= {new CourseClass("5","高等數(shù)學(xué)"),new CourseClass("6","大學(xué)英語")}; ?? ??? ? ?coursesToSelect.addAll(2, Arrays.asList(Course1)); ?? ??? ? ?CourseClass temp5 = (CourseClass)coursesToSelect.get(2); ?? ??? ? ?CourseClass temp6 = (CourseClass)coursesToSelect.get(3); ?? ??? ? ?//System.out.println("添加了兩門課程:"+temp5.id+":"+temp5.name+";"+temp6.id+":"+temp6.name); ? ?? ?} ?? ?public void testForEach() { ?? ??? ?System.out.println("可選擇的課程(通過FOREACH訪問):"); ?? ??? ?for(Object obj:coursesToSelect) { ?? ??? ??? ?CourseClass cr ?= (CourseClass) obj; ?? ??? ??? ?System.out.println("課程"+":"+cr.id+":"+cr.name); ?? ??? ?} ?? ?} ?? ?//查詢是否包含此課程 ?? ?public void testListContains() { ?? ??? ?CourseClass course = coursesToSelect.get(0); ?? ??? ?System.out.println("取得課程:"+course.name); ?? ??? ? ?? ??? ?System.out.println("備選課程是否包含課程:"+course.name+"," ?? ??? ??? ??? ?+coursesToSelect.contains(course)); ?? ??? ?System.out.println("請輸入課程名稱:"); ?? ??? ?String name = console.next(); ?? ??? ?CourseClass course2 = new CourseClass(); ?? ??? ?course2.name = name; ?? ??? ?System.out.println("新創(chuàng)建課程:"+course2.name); ?? ??? ?System.out.println("備選課程是否包含課程:"+course2.name+"," ?? ??? ??? ??? ?+coursesToSelect.contains(course2)); ?? ??? ?//indexof來求出索引 ?? ??? ??? ??? ?if(coursesToSelect.contains(course2)) { ?? ??? ??? ??? ??? ?System.out.println("課程:"+course2.name+"的索引位置為:"+ ?? ??? ??? ??? ?coursesToSelect.indexOf(course2)); ?? ??? ??? ??? ?} ?? ?} ?? ?public void creatStudentAndSelect() { ?? ??? ?//創(chuàng)建一個學(xué)生對象 ?? ??? ??? ??? ? student = new StudentsClass("1","小明"); ?? ??? ??? ??? ?System.out.println("歡迎學(xué)生:"+student.name+"選課!"); ?? ??? ??? ??? ?//從鍵盤輸入課程 ?? ??? ??? ??? ?Scanner console = new Scanner(System.in); ?? ??? ??? ??? ?for(int i = 0;i<3;i++) { ?? ??? ??? ??? ??? ?System.out.println("請輸入課程ID"); ?? ??? ??? ??? ??? ?String courseId = console.next(); ?? ??? ??? ??? ??? ?for(CourseClass cr :coursesToSelect) { ?? ??? ??? ??? ??? ??? ?if(cr.id.equals(courseId)) { ?? ??? ??? ??? ??? ??? ??? ?student.courses.add(cr); ?? ??? ??? ??? ??? ??? ?student.courses.add(null); ?? ??? ??? ??? ??? ??? ?student.courses.add(cr); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ?} ?? ?//測試Set的contains方法 ?? ?public void testSetContains() { ?? ??? ?System.out.println("請輸入學(xué)生已選的課程名:"); ?? ??? ?String name = ?console.next(); ?? ??? ?CourseClass course2 = new CourseClass(); ?? ??? ?course2.name = name; ?? ??? ?System.out.println("新創(chuàng)建課程:"+course2.name); ?? ??? ?System.out.println("備選課程是否包含課程:"+course2.name+"," ?? ??? ??? ??? ?+student.courses.contains(course2));?? ??? ? ?? ?} ?? ?public static void main(String[] args) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?SetTest st = new SetTest(); ?? ??? ?st.testAdd(); ??? ??? ?st.testForEach(); ?? ??? ?st.testListContains(); //?? ??? ?st.creatStudentAndSelect(); //?? ??? ?st.testSetContains(); ?? ?} }
5、利用Map進行增加,刪除,查詢,修改操作
package 學(xué)生管理系統(tǒng); ? import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; ? public class MapTest { ?? ? public Map<String,StudentsClass>students; ?? ?//利用哈希鍵值對進行創(chuàng)建 ?? ?public MapTest() { ?? ?this.students = new HashMap<String,StudentsClass>(); } ?? ?//進行學(xué)生的插入操作 ?? ? public void testPut() { ?? ??? ? Scanner console = new Scanner (System.in); ?? ??? ? int i = 0; ?? ??? ? while(i<3) { ?? ??? ??? ? System.out.println("請輸入學(xué)生ID:"); ?? ??? ??? ? String ID = console.next(); ?? ??? ??? ? StudentsClass st = students.get(ID); ?? ??? ??? ? if(st == null) { ?? ??? ??? ??? ? System.out.println("請輸入學(xué)生姓名:"); ?? ??? ??? ??? ? String name = console.next(); ?? ??? ??? ??? ? StudentsClass newStudent = new StudentsClass(ID,name); ?? ??? ??? ??? ? students.put(ID,newStudent); ?? ??? ??? ??? ? System.out.println("成功添加學(xué)生:"+students.get(ID).name); ?? ??? ??? ??? ? i++; ?? ??? ??? ? }else { ?? ??? ??? ??? ? System.out.println("該學(xué)生ID已經(jīng)被占用!"); ?? ??? ??? ??? ? continue; ?? ??? ??? ? } ?? ??? ? } ?? ? } ?? ? //利用keyset方法將key值放在一個集合中,然后利用key值進行遍歷 ?? ? public void testKeySet() { ?? ??? ? Set<String>keySet = students.keySet(); ?? ??? ? //進行遍歷 ?? ??? ? for(String stuID:keySet) { ?? ??? ??? ? StudentsClass st = students.get(stuID); ?? ??? ??? ? if(st != null) { ?? ??? ??? ??? ? System.out.println("學(xué)生:"+st.name); ?? ??? ??? ? } ?? ??? ? } ?? ? } ?? ? //進行刪除操作 ?? ? public void testRmove() { ?? ??? ? Scanner console = new Scanner(System.in); ?? ??? ? while(true) { ?? ??? ? System.out.println("請輸入要刪除的學(xué)生ID!"); ?? ??? ? String ID = console.next(); ?? ??? ? StudentsClass st = students.get(ID); ?? ??? ? ?if(st == null) { ?? ??? ??? ? ?System.out.println("該ID不存在!"); ?? ??? ??? ? ?continue; ?? ??? ? ?} ?? ??? ? ?students.remove(ID); ?? ??? ? ?System.out.println("成功刪除學(xué)生:"+st.name); ?? ??? ? ?break; ?? ? } } ?? ? //通過entryset來遍歷Map ?? ? public void testEntrySet() { ?? ??? ? Set<Entry<String,StudentsClass>>entrySet = students.entrySet(); ?? ??? ?for(Entry<String,StudentsClass> entry:entrySet) { ?? ??? ??? ??? ?System.out.println("取得鍵:"+entry.getKey()); ?? ??? ??? ??? ?System.out.println("對應(yīng)的值為:"+entry.getValue().name); ?? ??? ?}? ?? ? } ?? ? //進行修改操作 ?? ? public void testModify() { ?? ??? ? System.out.println("請輸入要修改的學(xué)生ID:"); ?? ??? ? Scanner console = new Scanner(System.in); ?? ??? ? while(true) { ?? ??? ??? ? String stuID = console.next(); ?? ??? ??? ? StudentsClass student = students.get(stuID); ?? ??? ??? ? if(student == null) { ?? ??? ??? ??? ? System.out.println("該ID不存在!請重新輸入!"); ?? ??? ??? ??? ? continue; ?? ??? ??? ? } ?? ??? ??? ? System.out.println("當(dāng)前學(xué)生ID,所對應(yīng)的學(xué)生為:"+student.name); ?? ??? ??? ? System.out.println("請輸入新的學(xué)生姓名:"); ?? ??? ??? ? String name = ?console.next(); ?? ??? ??? ? StudentsClass newStudent = new StudentsClass(stuID,name); ?? ??? ??? ? students.put(stuID,newStudent); ?? ??? ??? ? System.out.println("修改成功"); ?? ??? ? } ?? ? } ?? ? //通過鍵值或值進行查詢操作 ?? ? public void testContainsKeyOrValue() { ?? ??? ? System.out.println("請輸入要查詢的學(xué)生ID:"); ?? ??? ? Scanner console = new Scanner(System.in); ?? ??? ? String id = console.next(); ?? ??? ? System.out.println("您輸入的學(xué)生ID為:"+id+"在學(xué)生映射表中是否存在:"+ ?? ??? ? students.containsKey(id) ?? ??? ??? ??? ? ); ?? ??? ? if(students.containsKey(id)) { ?? ??? ??? ? System.out.println("對應(yīng)的學(xué)生為:"+students.get(id).name); ?? ??? ? } ?? ??? ? System.out.println("請輸入要查詢的學(xué)生姓名:"); ?? ??? ? String name = console.next(); ?? ??? ? if(students.containsValue(new StudentsClass(null,name))) ?? ??? ??? ? System.out.println("在學(xué)生映射表中確實包含學(xué)生:"+name); ?? ??? ? else ?? ??? ??? ? System.out.println("在學(xué)生映射表中不存在該學(xué)生"); ?? ? } ?? ?public static void main(String[] args) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?MapTest mt = new MapTest(); ?? ??? ?mt.testPut(); ?? ??? ?mt.testKeySet(); //?? ??? ?mt.testRmove(); //?? ??? ?mt.testEntrySet(); //?? ??? ?mt.testModify(); //?? ??? ?mt.testEntrySet(); ?? ??? ?mt.testContainsKeyOrValue(); ?? ?} }
6、進行重載比較
package 學(xué)生管理系統(tǒng); ? import java.util.Comparator; ? public class StudentComparator implements Comparator<StudentsClass> { ? ?? ?@Override ?? ?public int compare(StudentsClass o1, StudentsClass o2) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ? ?? ??? ? ?? ??? ?return o1.name.compareTo(o2.name); ?? ?} ? }
7、進行排序操作
package 學(xué)生管理系統(tǒng); ? import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; ? public class CollectionTest { ?? ? ?? ?//利用Collection內(nèi)部方法進行排序操作 ?? ?public void testSort1() { ?? ??? ?//進行整形排序 ?? ??? ?List<Integer>intergerList = new ArrayList<Integer>(); ?? ??? ?Random random = new Random(); ?? ??? ?Integer k; ?? ??? ?for(int i = 0;i < 10; i++) { ?? ??? ??? ? do { ?? ??? ??? ?k = random.nextInt(100); ?? ??? ??? ? }while(intergerList.contains(k)); ?? ??? ??? ? intergerList.add(k); ?? ??? ??? ? System.out.println("成功添加整數(shù):"+k); ?? ??? ??? ? System.out.println("--------排序前-------"); ?? ??? ??? ? for(Integer integer:intergerList) { ?? ??? ??? ??? ? System.out.println("元素:"+integer); ?? ??? ??? ? } ?? ??? ??? ? Collections.sort(intergerList); ?? ??? ??? ? System.out.println("--------排序后-------"); ?? ??? ??? ? for(Integer integer:intergerList)? ?? ??? ??? ??? ? System.out.println("元素:"+integer); ?? ??? ?} ?? ?} ?? ?//進行String排序 ?? ?public void testSort2() { ?? ??? ?List<String>stringList = new ArrayList<String>(); ?? ??? ?stringList.add("microsoft"); ?? ??? ?stringList.add("google"); ?? ??? ?stringList.add("lenovo"); ?? ??? ?System.out.println("--------排序前-------"); ?? ??? ?for(String string : stringList) { ?? ??? ??? ?System.out.println("元素:"+string); ?? ??? ?} ?? ??? ?Collections.sort(stringList); ?? ??? ?System.out.println("--------排序后-------"); ?? ??? ?for(String string : stringList) { ?? ??? ??? ?System.out.println("元素:"+string); ?? ??? ?} ?? ?} ?? ?//對StudentsClass類進行排序,這里就需要進行equal和hashcode的重載 ?? ?public void testSort3() { ?? ??? ?List<StudentsClass>StudentList = new ArrayList<StudentsClass>(); ?? ??? ?Random random = new Random(); ?? ??? ?StudentList.add(new StudentsClass(random.nextInt(1000)+"","mike")); ?? ??? ?StudentList.add(new StudentsClass(random.nextInt(1000)+"","lucy")); ?? ??? ?StudentList.add(new StudentsClass(random.nextInt(1000)+"","angel")); ?? ??? ?System.out.println("--------排序前-------"); ?? ??? ?for(StudentsClass student:StudentList) { ?? ??? ??? ?System.out.println("學(xué)生:"+student.id+student.name); ?? ??? ?} ?? ??? ?Collections.sort(StudentList); ?? ??? ?System.out.println("--------排序后-------"); ?? ??? ?for(StudentsClass student:StudentList) { ?? ??? ??? ?System.out.println("學(xué)生:"+student.id+student.name); ?? ??? ?} ?? ??? ?Collections.sort(StudentList,new StudentComparator()); ?? ??? ?System.out.println("----------按照姓名排序----------"); ?? ??? ?for(StudentsClass student:StudentList) { ?? ??? ??? ?System.out.println("學(xué)生:"+student.id+student.name); ?? ??? ?} ?? ?} ?? ?public static void main(String[] args) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?CollectionTest ct = new CollectionTest(); ?? ??? ?//ct.testSort1(); ?? ??? ?//ct.testSort2(); ?? ??? ?ct.testSort3(); ?? ?} ? }
8、輔助類。
package 學(xué)生管理系統(tǒng); ? public class ChildCourse extends CourseClass {?? ?? ? }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java+mysql實現(xiàn)學(xué)籍管理系統(tǒng)
- Java超詳細(xì)教你寫一個學(xué)籍管理系統(tǒng)案例
- java實現(xiàn)簡易的學(xué)籍管理系統(tǒng)
- java實現(xiàn)學(xué)籍管理系統(tǒng)
- Java+Mysql學(xué)生管理系統(tǒng)源碼
- javaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)
- Java+MySQL實現(xiàn)學(xué)生信息管理系統(tǒng)源碼
- java學(xué)生管理系統(tǒng)界面簡單實現(xiàn)(全)
- java學(xué)生信息管理系統(tǒng)源代碼
- java基于控制臺的學(xué)生學(xué)籍管理系統(tǒng)
相關(guān)文章
java多態(tài)實現(xiàn)電子寵物系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java多態(tài)實現(xiàn)電子寵物系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02創(chuàng)建動態(tài)代理對象bean,并動態(tài)注入到spring容器中的操作
這篇文章主要介紹了創(chuàng)建動態(tài)代理對象bean,并動態(tài)注入到spring容器中的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02SpringBoot 使用 Ehcache 作為緩存的操作方法
這篇文章主要介紹了SpringBoot 如何使用 Ehcache 作為緩存,我們通過添加 Ehcache 依賴、創(chuàng)建 Ehcache 配置文件并在 Spring Boot 應(yīng)用程序的配置文件中啟用 Ehcache 緩存,來配置 Ehcache 緩存,需要的朋友可以參考下2023-06-06基于java web獲取網(wǎng)頁訪問次數(shù)代碼實例
這篇文章主要介紹了基于java web獲取網(wǎng)頁訪問次數(shù)代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02SpringMVC中使用@PathVariable綁定路由中的數(shù)組的方法
這篇文章主要介紹了SpringMVC中使用@PathVariable綁定路由中的數(shù)組的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07