紫金湖人
2016-03-08 17:30
package chooseclass;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
/**
* Created by Administrator on 2016-03-08.
*/
public class SetTest {
public List<Course> coursesToSelect;
public SetTest(){
coursesToSelect=new ArrayList<Course>();
}
public void testAdd(){
Course cr1 = new Course();
cr1.setId("0");
cr1.setName("JAVA编程");
coursesToSelect.add(cr1);
Course temp=(Course)coursesToSelect.get(0);
Course cr2=new Course();
cr2.setId("1");
cr2.setName("大学英语");
coursesToSelect.add(1, cr2);
Course temp1=(Course)coursesToSelect.get(1);
Course cr3=new Course();
cr3.setId("2");
cr3.setName("计算机导论");
coursesToSelect.add(cr3);
Course temp2=(Course)coursesToSelect.get(2);
System.out.println();
}
public void testIterator(){
Iterator it=coursesToSelect.iterator();
System.out.println("通过迭代器访问:");
while (it.hasNext()){
Course cr=(Course)it.next();
System.out.println(cr.getId() + cr.getName());
}
System.out.println();
}
public static void main(String[] args){
SetTest st=new SetTest();
st.testAdd();
st.testIterator();
Student student=new Student();
student.setId("0");
student.setName("小翳");
System.out.println("欢迎" + student.getName() + "同学选课");
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.getId().equals(courseID)){
student.courses.add(cr);
}
}
}
st.testForEachForSet(student);
}
public void testForEachForSet(Student student){
for (Course cr:student.courses){
System.out.println("选择了课程:"+cr.getId()+cr.getName());
}
}
}
package chooseclass;
import java.security.PublicKey;
import java.util.HashSet;
import java.util.Set;
/**
* Created by Administrator on 2016-03-07.
*/
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String id;
public String getId()
{
return id;
}
public void setId(String id) {
this.id = id;
}
public Set <Course>courses;
public Set getCourses() {
return courses;
}
public void setCourses(Set courses) {
this.courses = new HashSet<Course>();
}
}
public Set <Course>courses;
public Set getCourses() {
return courses;
}
public void setCourses(Set courses) {
this.courses = new HashSet<Course>();
}
改为
public Set <Course>courses = new HashSet<Course>();
Java入门第三季
409792 学习 · 4340 问题
相似问题