ChildCourse.java
package com.imooc;
public class ChildCourse extends Course {
}
Course
package com.imooc;
/**
* 課程類
* @author Administrator
*
*/
public class Course {
public String id;
public String name;
public Course(String id, String name) {
this.id = id ;
this.name = name;
}
public Course() {
}
@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 Course))
return false;
Course other = (Course) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
ListTest.java
package com.imooc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* 備選課程類
* @author Administrator
*
*/
public class ListTest {
/**
* 用于存放備選課程的List
*/
public List coursesToSelect;
public ListTest() {
this.coursesToSelect = new ArrayList();
}
/**
* 用于往coursesToSelect中添加備選課程
*/
public void testAdd() {
//創(chuàng)建一個(gè)課程對象莺匠,并通過調(diào)用add方法凄诞,添加到備選課程List中
Course cr1 = new Course("1" , "數(shù)據(jù)結(jié)構(gòu)");
coursesToSelect.add(cr1);
Course temp = (Course) coursesToSelect.get(0);
System.out.println("添加了課程:" + temp.id + ":" + temp.name);
Course cr2 = new Course("2", "C語言");
coursesToSelect.add(0, cr2);
Course temp2 = (Course) coursesToSelect.get(0);
System.out.println("添加了課程:" + temp2.id + ":" + temp2.name);
coursesToSelect.add(cr1);
Course temp0 = (Course) coursesToSelect.get(2);
System.out.println("添加了課程:" + temp.id + ":" + temp.name);
//以下方法會拋出數(shù)組下標(biāo)越界異常
// Course cr3 = new Course("3", "test");
// coursesToSelect.add(4, cr3);
Course[] course = {new Course("3", "離散數(shù)學(xué)"), new Course("4", "匯編語言")};
coursesToSelect.addAll(Arrays.asList(course));
Course temp3 = (Course) coursesToSelect.get(2);
Course temp4 = (Course) coursesToSelect.get(3);
System.out.println("添加了兩門課程:" + temp3.id + ":" +
temp3.name + ";" + temp4.id + ":" + temp4.name);
Course[] course2 = {new Course("5", "高等數(shù)學(xué)"), new Course("6", "大學(xué)英語")};
coursesToSelect.addAll(2, Arrays.asList(course2));
Course temp5 = (Course) coursesToSelect.get(2);
Course temp6 = (Course) coursesToSelect.get(3);
System.out.println("添加了兩門課程:" + temp5.id + ":" +
temp5.name + ";" + temp6.id + ":" + temp6.name);
}
/**
* 取得List中的元素的方法
* @param args
*/
public void testGet() {
int size = coursesToSelect.size();
System.out.println("有如下課程待選:");
for(int i= 0 ; i < size; i++) {
Course cr = (Course) coursesToSelect.get(i);
System.out.println("課程:" + cr.id + ":" + cr.name);
}
}
/**
* 通過迭代器來遍歷List
* @param args
*/
public void testIterator() {
//通過集合的iterator方法,取得迭代器的實(shí)例
Iterator<Course> it = coursesToSelect.iterator();
System.out.println("有如下課程待選(通過迭代器訪問):");
while(it.hasNext()) {
Course cr = it.next();
System.out.println("課程:" + cr.id + ":" + cr.name);
}
}
/**
* 通過for each方法訪問集合元素
* @param args
*/
public void testForEach() {
System.out.println("有如下課程待選(通過for each訪問):");
for (Object obj : coursesToSelect) {
Course cr = (Course) obj;
System.out.println("課程:" + cr.id + ":" + cr.name);
}
}
/**
* 修改List中的元素
* @param args
*/
public void testModify() {
coursesToSelect.set(4, new Course("7", "毛概"));
}
/**
* 刪除List中的元素
* @param args
*/
public void testRemove() {
// Course cr = (Course) coursesToSelect.get(4);
System.out.println("即將刪除4位置和5位置上的課程市咆!");
Course[] courses = {(Course) coursesToSelect.get(4), (Course) coursesToSelect.get(5)};
coursesToSelect.removeAll(Arrays.asList(courses));
// coursesToSelect.remove(4);
System.out.println("成功刪除課程!");
testForEach();
}
/**
* 往List中添加一些奇怪的東西
* @param args
*/
public void testType() {
System.out.println("能否往List中添加一些奇怪的東西呢?鹘稀懦铺?");
coursesToSelect.add("我不是課程,我只是一個(gè)無辜的字符串N粞ā!");
}
public static void main( String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testType();
lt.testForEach();
// lt.testGet();
// lt.testIterator();
// lt.testForEach();
// lt.testModify();
// lt.testForEach();
// lt.testRemove();
}
}
MapTest.java
package com.imooc;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
/**
* 用來承裝學(xué)生類型對象
*/
public Map<String, Student> students;
/**
* 在構(gòu)造器中初始化students屬性
*/
public MapTest() {
this.students = new HashMap<String, Student>();
}
/**
* 測試添加:輸入學(xué)生ID提前,判斷是否被占用
* 若未被占用吗货,則輸入姓名,創(chuàng)建新學(xué)生對象狈网,并且
* 添加到students中
*/
public void testPut() {
// 創(chuàng)建一個(gè)Scanner對象宙搬,用來獲取輸入的學(xué)生ID和姓名
Scanner console = new Scanner(System.in);
int i = 0;
while (i < 3) {
System.out.println("請輸入學(xué)生ID:");
String ID = console.next();
// 判斷該ID是否被占用
Student st = students.get(ID);
if (st == null) {
// 提示輸入學(xué)生姓名
System.out.println("請輸入學(xué)生姓名:");
String name = console.next();
// 創(chuàng)建新的學(xué)生對象
Student newStudent = new Student(ID, name);
// 通過調(diào)用students的put方法,添加ID-學(xué)生映射
students.put(ID, newStudent);
System.out.println("成功添加學(xué)生:" + students.get(ID).name);
i++;
} else {
System.out.println("該學(xué)生ID已被占用拓哺!");
continue;
}
}
}
/**
* 測試Map的keySet方法
*/
public void testKeySet() {
// 通過keySet方法勇垛,返回Map中的所有“鍵”的Set集合
Set<String> keySet = students.keySet();
// 取得students的容量
System.out.println("總共有:" + students.size() + "個(gè)學(xué)生!");
// 遍歷keySet士鸥,取得每一個(gè)鍵闲孤,再調(diào)用get方法取得每個(gè)鍵對應(yīng)的value
for (String stuId : keySet) {
Student st = students.get(stuId);
if (st != null)
System.out.println("學(xué)生:" + st.name);
}
}
/**
* 測試刪除Map中的映射
*/
public void testRemove() {
// 獲取從鍵盤輸入的待刪除學(xué)生ID字符串
Scanner console = new Scanner(System.in);
while (true) {
// 提示輸入待刪除的學(xué)生的ID
System.out.println("請輸入要?jiǎng)h除的學(xué)生ID!");
String ID = console.next();
// 判斷該ID是否有對應(yīng)的學(xué)生對象
Student st = students.get(ID);
if (st == null) {
// 提示輸入的ID并不存在
System.out.println("該ID不存在础淤!");
continue;
}
students.remove(ID);
System.out.println("成功刪除學(xué)生:" + st.name);
break;
}
}
/**
* 通過entrySet方法來遍歷Map
*/
public void testEntrySet() {
// 通過entrySet方法崭放,返回Map中的所有鍵值對
Set<Entry<String, Student>> entrySet = students.entrySet();
for (Entry<String, Student> entry : entrySet) {
System.out.println("取得鍵:" + entry.getKey());
System.out.println("對應(yīng)的值為:" + entry.getValue().name);
}
}
/**
* 利用put方法修改Map中的已有映射
*/
public void testModify() {
// 提示輸入要修改的學(xué)生ID
System.out.println("請輸入要修改的學(xué)生ID:");
// 創(chuàng)建一個(gè)Scanner對象,去獲取從鍵盤上輸入的學(xué)生ID字符串
Scanner console = new Scanner(System.in);
while (true) {
// 取得從鍵盤輸入的學(xué)生ID
String stuID = console.next();
// 從students中查找該學(xué)生ID對應(yīng)的學(xué)生對象
Student student = students.get(stuID);
if (student == null) {
System.out.println("該ID不存在鸽凶!請重新輸入币砂!");
continue;
}
// 提示當(dāng)前對應(yīng)的學(xué)生對象的姓名
System.out.println("當(dāng)前該學(xué)生ID,所對應(yīng)的學(xué)生為:" + student.name);
// 提示輸入新的學(xué)生姓名玻侥,來修改已有的映射
System.out.println("請輸入新的學(xué)生姓名:");
String name = console.next();
Student newStudent = new Student(stuID, name);
students.put(stuID, newStudent);
System.out.println("修改成功决摧!");
break;
}
}
/**
* 測試Map中,是否包含某個(gè)Key值或者某個(gè)Value值
*/
public void testContainsKeyOrValue() {
// 提示輸入學(xué)生id
System.out.println("請輸入要查詢的學(xué)生ID:");
Scanner console = new Scanner(System.in);
String id = console.next();
// 在Map中,用containsKey()方法掌桩,來判斷是否包含某個(gè)Key值
System.out.println("您輸入的學(xué)生ID為:" + id + "边锁,在學(xué)生映射表中是否存在:"
+ students.containsKey(id));
if (students.containsKey(id))
System.out.println("對應(yīng)的學(xué)生為:" + students.get(id).name);
// 提示輸入學(xué)生姓名
System.out.println("請輸入要查詢的學(xué)生姓名:");
String name = console.next();
// 用containsValue()方法,來判斷是否包含某個(gè)Value值
if (students.containsValue(new Student(null,name)))
System.out.println("在學(xué)生映射表中波岛,確實(shí)包含學(xué)生:" + name);
else
System.out.println("在學(xué)生映射表中不存在該學(xué)生茅坛!");
}
/**
* @param args
*/
public static void main(String[] args) {
MapTest mt = new MapTest();
mt.testPut();
mt.testKeySet();
// mt.testRemove();
// mt.testEntrySet();
// mt.testModify();
// mt.testEntrySet();
mt.testContainsKeyOrValue();
}
}
SetTest.java
package com.imooc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SetTest {
public List<Course> coursesToSelect;
private Scanner console;
public Student student;
public SetTest() {
coursesToSelect = new ArrayList<Course>();
console = new Scanner(System.in);
}
/**
* 用于往coursesToSelect中添加備選課程
*/
public void testAdd() {
//創(chuàng)建一個(gè)課程對象,并通過調(diào)用add方法则拷,添加到備選課程List中
Course cr1 = new Course("1" , "數(shù)據(jù)結(jié)構(gòu)");
coursesToSelect.add(cr1);
Course temp = (Course) coursesToSelect.get(0);
// System.out.println("添加了課程:" + temp.id + ":" + temp.name);
Course cr2 = new Course("2", "C語言");
coursesToSelect.add(0, cr2);
Course temp2 = (Course) coursesToSelect.get(0);
// System.out.println("添加了課程:" + temp2.id + ":" + temp2.name);
// coursesToSelect.add(cr1);
// Course temp0 = (Course) coursesToSelect.get(2);
// System.out.println("添加了課程:" + temp.id + ":" + temp.name);
//以下方法會拋出數(shù)組下標(biāo)越界異常
// Course cr3 = new Course("3", "test");
// coursesToSelect.add(4, cr3);
Course[] course = {new Course("3", "離散數(shù)學(xué)"), new Course("4", "匯編語言")};
coursesToSelect.addAll(Arrays.asList(course));
Course temp3 = (Course) coursesToSelect.get(2);
Course temp4 = (Course) coursesToSelect.get(3);
// System.out.println("添加了兩門課程:" + temp3.id + ":" +
// temp3.name + ";" + temp4.id + ":" + temp4.name);
Course[] course2 = {new Course("5", "高等數(shù)學(xué)"), new Course("6", "大學(xué)英語")};
coursesToSelect.addAll(2, Arrays.asList(course2));
Course temp5 = (Course) coursesToSelect.get(2);
Course temp6 = (Course) coursesToSelect.get(3);
// System.out.println("添加了兩門課程:" + temp5.id + ":" +
// temp5.name + ";" + temp6.id + ":" + temp6.name);
}
/**
* 通過for each方法訪問集合元素
* @param args
*/
public void testForEach() {
System.out.println("有如下課程待選(通過for each訪問):");
for (Object obj : coursesToSelect) {
Course cr = (Course) obj;
System.out.println("課程:" + cr.id + ":" + cr.name);
}
}
/**
* 測試List的contains方法
*/
public void testListContains() {
// 取得備選課程序列的第0個(gè)元素
Course course = coursesToSelect.get(0);
// 打印輸出coursesToSelected是否包含course對象
System.out.println("取得課程:" + course.name);
System.out.println("備選課程中是否包含課程:" + course.name + ", " +
coursesToSelect.contains(course));
// 提示輸入課程名稱
System.out.println("請輸入課程名稱:");
String name = console.next();
// 創(chuàng)建一個(gè)新的課程對象贡蓖,ID和名稱,與course對象完全一樣
Course course2 = new Course();
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));
}
// 創(chuàng)建學(xué)生對象并選課
public void createStudentAndSelectCours() {
//創(chuàng)建一個(gè)學(xué)生對象
student = new Student("1", "小明");
System.out.println("歡迎學(xué)生:" + student.name + "選課煌茬!");
//創(chuàng)建一個(gè)Scanner對象斥铺,用來接收從鍵盤輸入的課程ID
Scanner console = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("請輸入課程ID");
String courseId = console.next();
for (Course cr : coursesToSelect) {
if(cr.id.equals(courseId)) {
student.courses.add(cr);
/**
* Set中,添加某個(gè)對象坛善,無論添加多少次晾蜘,
* 最終只會保留一個(gè)該對象(的引用),
* 并且眠屎,保留的是第一次添加的那一個(gè)
*/
// student.courses.add(null);
// student.courses.add(cr);
}
}
}
}
/**
* 測試Set的contains方法
*/
public void testSetContains() {
// 提示輸入課程名稱
System.out.println("請輸入學(xué)生已選的課程名稱:");
String name = console.next();
// 創(chuàng)建一個(gè)新的課程對象剔交,ID和名稱,與course對象完全一樣
Course course2 = new Course();
course2.name = name;
System.out.println("新創(chuàng)建課程:" + course2.name);
System.out.println("備選課程中是否包含課程:" + course2.name + ", " +
student.courses.contains(course2));
}
/**
* @param args
*/
public static void main(String[] args) {
SetTest st = new SetTest();
st.testAdd();
st.testListContains();
st.testForEach();
// st.createStudentAndSelectCours();
// st.testSetContains();
//
// st.testForEachForSet(student);
}
public void testForEachForSet(Student student) {
//打印輸出,學(xué)生所選的課程!
System.out.println("共選擇了:" + student.courses.size() + "門課程术陶!");
for (Course cr : student.courses) {
System.out.println("選擇了課程:" + cr.id + ":" + cr.name);
}
}
}
Student.java
package com.imooc;
import java.util.HashSet;
import java.util.Set;
/**
* 學(xué)生類
* @author Administrator
*
*/
public class Student implements Comparable<Student> {
public String id;
public String name;
public Set<Course> courses;
public Student(String id, String name) {
this.id = id;
this.name = name;
this.courses = new HashSet<Course>();
}
@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 Student))
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
//@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return this.id.compareTo(o.id);
}
}
StudentComparator.java
package com.imooc;
import java.util.Comparator;
public class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.name.compareTo(o2.name);
}
}
TestGeneric.java
package com.imooc;
import java.util.ArrayList;
import java.util.List;
public class TestGeneric {
/**
* 帶有泛型——Course缘缚,的List類型屬性
*/
public List<Course> courses;
public TestGeneric() {
this.courses = new ArrayList<Course>();
}
/**
* 測試添加
*/
public void testAdd() {
Course cr1 = new Course("1","大學(xué)語文");
courses.add(cr1);
//泛型集合中,不能添加泛型規(guī)定的類型及其子類型以外的對象阿宅,否則會報(bào)錯(cuò)候衍!
// courses.add("能否添加一些奇怪的東西呢?洒放?");
Course cr2 = new Course("2","Java基礎(chǔ)");
courses.add(cr2);
}
/**
* 測試循環(huán)遍歷
*/
public void testForEach() {
for (Course cr : courses) {
System.out.println(cr.id + ":" + cr.name);
}
}
/**
* 泛型結(jié)合可以添加泛型的子類型的對象實(shí)例
*/
public void testChild() {
ChildCourse ccr = new ChildCourse();
ccr.id = "3";
ccr.name = "我是子類型的課程對象實(shí)例~~";
courses.add(ccr);
}
/**
* 泛型不能使用基本類型
*/
public void testBasicType() {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
System.out.println("基本類型必須使用包裝類作為泛型蛉鹿!" + list.get(0));
}
/**
* @param args
*/
public static void main(String[] args) {
TestGeneric tg = new TestGeneric();
tg.testAdd();
tg.testForEach();
tg.testChild();
tg.testForEach();
tg.testBasicType();
}
}