基础功能:
1.通过Map对学生信息进行管理
2.通过键盘输入学生信息
3.对集合中学生的信息进行增删改操作
步骤一:先创建学生类和课程类
学生类
import java.util.HashSet;
import java.util.Set;
public class Student { //学生类
public String id;
public String name; //公有
public Set courses;
public Student(String id,String name) { //添加构造器
this.id = id;
this.name = name;
this.courses = new HashSet();
}
}
课程类
public class Course { //课程类
public String id; //公有
public String name;
public Course(String id,String name){ //添加构造器
this.id = id;
this.name = name;
}
}
步骤二:添加课程
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListTest { //备选课程类
public List coursesToSelect; //用于存放备选课程的List
public ListTest() {
this.coursesToSelect = new ArrayList();
}
public void testAdd() { //用于往coursesToSelect中添加备选课程
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course cr1 = new Course("1","web");
coursesToSelect.add(cr1);
Course temp = (Course) coursesToSelect.get(0);
System.out.println("添加了课程:" + temp.id +"."+temp.name);
Course cr2 = new Course("2","java");
coursesToSelect.add(0,cr2);
Course temp2 = (Course) coursesToSelect.get(0);
System.out.println("添加了课程:" + temp2.id +"."+temp2.name);
Course[] course = {new Course("3","计算机基础"),new Course("4","ps")};//用数组方法添加
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);
}
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
}
}
步骤三:课程查询
import java.util.Iterator;
public void testGet() { //取得List中的元素方法
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);
}
public void testIterator() { //通过迭代器来遍历List
//通过集合的iterator方法,取得迭代器的实例
Iterator it = coursesToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while(it.hasNext()) {
Course cr = (Course)it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
public void testForEach() {
System.out.println("有如下课程待选(通过for each访问):");
for(Object obj:coursesToSelect) {
Course cr = (Course) obj;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
lt.testGet();
lt.testIterator();
步骤四:课程修改,课程删除
修改:
/**
* 修改List中的元素
* @param args
* public void testModify(){
* coursesToSelect.set(索引位置,new Course(新的课程对象)("新id","新课程");
* 调用方法:
* lt.testModify();
* lt.testForEach();
* }
*/
删除:
/**
* 课程删除
* @param args
* public void testRemove(){
* Course cr = (Course) coursesToSelect.get(4);
* System.out.println("即将删除的课程");
* coursesToSelect.remove(4);
* System.out.println("成功删除课程");
* testForEach();(调用的方法)
* lt.testRemove();(在main中调用的方法)
* }
*/
步骤五:创建学生,学生自己选课
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SetTest {
public List<Course> coursesToSelect;
public SetTest() {
coursesToSelect = new ArrayList<Course>();
}
public void testAdd() { //用于往coursesToSelect中添加备选课程
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course cr1 = new Course("1","web");
coursesToSelect.add(cr1);
Course temp = (Course) coursesToSelect.get(0);
//System.out.println("添加了课程:" + temp.id +"."+temp.name);
Course cr2 = new Course("2","java");
coursesToSelect.add(0,cr2);
Course temp2 = (Course) coursesToSelect.get(0);
//System.out.println("添加了课程:" + temp2.id +"."+temp2.name);
Course[] course = {new Course("3","计算机基础"),new Course("4","ps")};//用数组方法添加
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);
}
public void testForEach() {
System.out.println("有如下课程待选(通过for each访问):");
for(Object obj:coursesToSelect) {
Course cr = (Course) obj;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
public static void main(String[] args) {
SetTest st = new SetTest();
st.testAdd();
st.testForEach();
//创建一个学生对象
Student student = new Student("1","小明");
System.out.println("欢迎学生:"+student.name+"选课");
//创建一个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 :st.coursesToSelect) {
if(cr.id.equals(courseId)) {
student.courses.add(cr);
}
}
}
st.testForEachForSet(student);
}
public void testForEachForSet(Student student) {
//打印输出,学生所选的课程
for(Course cr:student.courses) {
System.out.println("选择课程:"+cr.id+":"+cr.name);
}
}
}
步骤六:使用Map增加、删除、修改学生
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MapText {
public Map<String,Student>students;//用来承装学生类型对象
public MapText() { //在构造器中初始化students属性
this.students = new HashMap<String,Student>();
}
//测试添加:输入学生ID,判断是否被占用
//若未被占用,则输入姓名,创建新学生对象,并且添加到students中
public void testPut() {
//创建一个Scanner对象,用来获取输入的学生ID和姓名
Scanner console = new Scanner(System.in);
int i = 0;
while(i<3) {
System.out.println("请输入学生ID:");
String ID = console.next();
//判断该ID是否被占用
Student st = students.get(ID);
if(st==null) {
//提示输出学生名字
System.out.println("请输入学生姓名");
String name = console.next();
//创建新的学生对象
Student newStudent = new Student(ID,name);
//通过通过students的put方法,添加ID-学生映射
students.put(ID, newStudent);
System.out.println("成功添加学生:"+students.get(ID).name);
i++;
}else {
System.out.println("该学生ID已被占用!");
continue;
}
}
}
//测试Map的keySet方法
public void testKeySet() {
//通过KeySet方法,返回Map中的所有“键”的Set集合
Set<String> keySet = students.keySet();
//取得students的容量
System.out.println("总共有:"+ students.size()+"个学生");
//遍历KeySet,取得每一个键,再调用get方法取得每个键对应的value
for(String stuId:keySet) {
Student st = students.get(stuId);
if(st!=null)
System.out.println("学生:"+st.name);
}
}
//测试删除Map中的映射
public void testRemove() {
//获取从键盘输入的待删除学生ID字符串
Scanner console = new Scanner(System.in);
while(true) {
//提示输入待删除的学生的ID
System.out.println("请输入要删除的学生ID。");
String ID = console.next();
//判断该ID是否有对应的学生对象
Student st = students.get(ID);
if(st==null) {
//提示输入的ID不存在
System.out.println("该ID不存在");
continue;
}
students.remove(ID);
System.out.println("成功删除学生:"+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("对应的值为:"+entry.getValue());
}
}
//利用put方法修改Map中的已有映射
public void testModify() {
//提示输入要修改的学生ID
System.out.println("请输入要修改的学生ID:");
//创建一个Scanner对象,去获取从键盘上输入的学生ID字符串
Scanner console = new Scanner(System.in);
while(true) {
//取得从键盘输入的学生ID
String stuID = console.next();
//从students中查找该学生ID对应的学生对象
Student student = students.get(stuID);
if(student==null) {
System.out.println("该ID不存在!请重新输入!");
continue;
}
//提示当前对应的学生对象姓名
System.out.println("当前该学生ID,所对应的学生为:"+student.name);
//提示输入新的学生姓名,来修改已有的映射
System.out.println("请输入新的学生姓名:");
String name = console.next();
Student newStudent = new Student(stuID,name);
students.put(stuID, newStudent);
System.out.println("修改成功!");
break;
}
}
public static void main(String[] args) {
//调用方法
MapText mt = new MapText();
mt.testPut();
mt.testKeySet();
mt.testRemove();
mt.testEntrySet();
mt.testModify();
mt.testEntrySet();
}
}