SetTest文件報錯
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("大學(xué)英語");
? ? ? ?coursesToSelect.add(1, cr2);
? ? ? ?Course temp1=(Course)coursesToSelect.get(1);
? ? ? ?Course cr3=new Course();
? ? ? ?cr3.setId("2");
? ? ? ?cr3.setName("計算機導(dǎo)論");
? ? ? ?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() + "同學(xué)選課");
? ? ? ?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>();
? ?}
}
2016-03-08
?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>();