2012-03-01 206 views
2

我有一些問題我有兩個類,每個都返回一個Iterator,還有一個類返回名爲Student的值。有了這段代碼,我可以迭代一個類,我想知道是否有一種方法可以在另一個類上迭代而不需要在我的Machine類中添加第二個類。這裏是我的代碼看起來是這樣的:只用一個迭代器迭代兩個列表Java

import java.util.*; 
import java.lang.reflect.*; 

class ClassRoom1{ 
    ArrayList<Student> al = new ArrayList<Student>(); 
    Student st; 
    public ClassRoom1(){ 
     st = new Student("Michael", "Smith", 12); 
     al.add(st); 
     st = new Student("Jennifer", "Lopez", 13); 
     al.add(st); 
    } 

    public void addStudent(String name, String lastName, int age) { 
     Student st = new Student(name, lastName, age); 
     al.add(st); 
    } 

    public ArrayList getStudents(){ 
     return al; 
    } 

    public Iterator returnIter(){ 
     Iterator iter = getStudents().iterator(); 
     return iter; 
    } 
} 

class ClassRoom2{ 
    ArrayList<Student> al = new ArrayList<Student>(); 
    Student st; 
    public ClassRoom2(){ 
     st = new Student("Brian", "Con", 15); 
     al.add(st); 
     st = new Student("Megan", "Bell", 15); 
     al.add(st); 
    } 

    public void addStudent(String name, String lastName, int age) { 
     Student st = new Student(name, lastName, age); 
     al.add(st); 
    } 

    public ArrayList getStudents(){ 
     return al; 
    } 

    public Iterator returnIter(){ 
     Iterator iter = getStudents().iterator(); 
     return iter; 
    } 
} 

public class Machine{ 
    Student st; 
    ClassRoom1 clrm1 = new ClassRoom1(); 
    ClassRoom2 clrm2 = new ClassRoom2();  

    public static void main(String[] args){ 
     Machine mch = new Machine(); 
     ArrayList al = new ArrayList(); 
     Iterator iter = al.iterator(); 
     mch.printStudens(iter); 
    } 

    void printStudens(Iterator iter){ 
     iter = clrm1.returnIter(); 
     while(iter.hasNext()){ 
      st = (Student) iter.next(); 
      System.out.println("\nName: " + st.getName() + "\nLast name: " + st.getLastName() + "\nAge: " + st.getAge()); 
     } 
    } 
} 
+3

我會去教室1去見珍妮弗洛佩茲:) 此外,你應該在循環中使用兩個迭代器。 – vulkanino 2012-03-01 16:24:47

+0

你真的需要使用迭代器嗎? – hurtledown 2012-03-01 16:25:20

+0

我不太明白。你不必有一個內部循環,但你需要有另一個迭代器。你可以編輯你的問題,提供更多關於你在循環中試圖做什麼的信息嗎? – Gray 2012-03-01 16:28:28

回答

4

首先,代替代碼複製,使用OO:

class ClassRoom{ 
    private List<Student> al; 
    public ClassRoom1(List<Student> studentList){ 
     a1 = new ArrayList(studentList); 
    } 

    public void addStudent(String name, String lastName, int age) { 
     al.add(new Student(name, lastName, age)); 
    } 

    public List<Student> getStudents(){ 
     return al; 
    } 

    public Iterator<Student> returnIter(){ 
     return getStudents().iterator(); 
    } 
} 

然後clrm1clrm2是課堂的對象。實際上,您可以將任意多個ClassRoom對象保存在自己的列表中,並執行嵌套迭代,首先遍歷所有ClassRooms,您可以在其中循環訪問當前ClassRoom中的所有學生。


更新:

如果一旦需要迭代器(這裏沒有必要)結合起來,我想無論是我自己寫的小串接迭代其中,例如按順序迭代它所持有的迭代器,或者使用Guava的Iterator.concat(),它將「將多個迭代器組合成一個迭代器」。無論哪種方式,您都不需要複製任何數據。

0

我不會把與類似於它的類型名稱的變量(這可能會改變),但它包含:

ArrayList<Student> students = new ArrayList<Student>(); 

我會將addStudent方法更改爲接受Student,而不是姓名,姓氏和年齡。

public void addStudent(Student newStudent) 
    { 
      students.add(newStudent); 
    } 

然後,您可以添加學生,如:

class1.addStudent(new Student("jennifer", "lopez", 30)); 

而且還添加相同的學生不同的類別:

Student jennifer = new Student("jennifer", "lopez", 30); 
class1.addStudent(jennifer); 
class2.addStudent(jennifer); 

最後,是的,你需要兩個迭代器在環:)

0

您的設計完全錯誤。你不應該爲每個(學校)ClassRoom創建兩個不同的(Java)類。只要創建一個:

public class ClassRoom 
{ 
    ... all your stuff here 

    public void addStudent(Student st) 
    { 
     students.add(st); 
    } 

    public List<Student> getStudents() 
    { 
     return students; 
    } 
} 

然後,您可以創建這樣的多個類,並把它們放入數組:

List<ClassRoom> rooms = new ArrayList<ClassRoom>(); 

ClassRoom room1 = new ClassRoom("5 Latin Sience"); 
room1.addStudent(new Student("Anna", "Bell", 12)); 
room1.addStudent(new Student("Martin", "Foo", 12)); 
room1.addStudent(new Student("Stef", "Bar", 12)); 


ClassRoom room2 = new ClassRoom("4 Maths"); 
room2.addStudent(new Student("Anna", "Bell", 11)); 
room2.addStudent(new Student("Martin", "Foo", 11)); 
room2.addStudent(new Student("Stef", "Bar", 15)); 

// put them int the array. 
rooms.add(room1); 
rooms.add(room2); 

現在,你可以遍歷每個教室,然後遍歷每個學生有嵌套的for循環:

for (ClassRoom room : rooms) 
{ 
    for (Student st : room.getStudents()) 
    { 
     System.out.println(... student details here ...); 
    } 
} 
0

我同意關於不好的設計問題的答案,但回答問題實際得到一個迭代器兩個列表下面的代碼應該工作。由於數組複製,這將是計算成本昂貴的,所以您可能想要使用鏈接列表來緩解這種情況。

clrm1.getStudents().clone().addAll(clrm2.getStudents()).iterator(); 

另一種選擇是使用Class對象的列表,學生名單,然後flattening iterator

0

我沒有看到兩個教室不同級別的原因:它們是相同的。

public class Classroom { 
    // each classroom has a list of students 
    List<Student> students = new ArrayList<Student>(); 

    // each classroom has a method to add a new student 
    public void addStudent(String name, String lastName, int age) { 
    Student st = new Student(name, lastName, age); 
    al.add(st); 
    } 

    // each classroom has a method to "show" his students 
    public List<Student> getStudents() { 
    return students; 
    } 
} 

現在你有一個

public class Machine{ 

    // a list of classrooms 
    List<Classroom> classRooms = new ArrayList<Classroom>(); 

    // just to start the application 
    public static void main(String[] args){ 
     Machine machine = new Machine(); 
     machine.printStudents(); 
    } 

    public Machine() { 
     // we initialize the classrooms 
     // Note - a machine wouldn't create classes, this is just for the demo 
     Classroom class1 = new Classroom(); 
     class1.addStudent("Michael", "Smith", 12); 
     class1.addStudent("Jennifer", "Lopez", 13); 
     classRooms.add(class1); 

     Classroom class2 = new Classroom(); 
     class2.addStudent("Brian", "Con", 15); 
     class2.addStudent("Brian", "Con", 15);   
     classRooms.add(class2); 
    } 

    void printStudents() { 
     for (Classroom class:classrooms) { 
     // one could print the classname at this place 
     for (Student student: class.getStudents()) { 
      System.out.println("\nName: " + st.getName() + "\nLast name: " + st.getLastName() + "\nAge: " + st.getAge()); 
     } 
     } 
    } 
} 

這是您的解決方案完全不同 - 只有一班教室,沒有迭代器(我們不需要它們)。嘗試一下並將其與您的代碼進行比較。