title: Iterate over two list with just one Iterator Java
body: <p>I'm having some problems I have two classes and each one returns a Iterator, there is a class that return the values named Student. With this code I can iterate over one class and I'd like to know if there is a way to iterate over the other class without adding a second while at my Machine class. Here is how my code looks like:</p>
  
  <pre><code>import java.util.*;
  import java.lang.reflect.*;
  
  class ClassRoom1{
      ArrayList&lt;Student&gt; al = new ArrayList&lt;Student&gt;();
      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&lt;Student&gt; al = new ArrayList&lt;Student&gt;();
      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());
          }
      }
  }
  </code></pre>