title: Java use instanceof with iterator
body: <p>I have an abstract class <code>Usuario</code>and an <code>ArrayList&lt;Usuario&gt;</code> with objects of its three subclasses. I now want to iterate through the <code>ArrayList</code> and return a value depending of the result of using <code>instanceof</code> against that object. </p>
  
  <p>I get an error: <code>java.util.NoSuchElementException</code>.</p>
  
  <p>I suppose it is because of the iterator being an object of <code>Iterator</code> and not from any of the subclasses from <code>Usuario</code>. Am I right? is there any solution for that?</p>
  
  <pre><code>public int comprobarDni(String dniAComprobar, ArrayList&lt;Usuario&gt; listaUsuarios) {
      Iterator&lt;Usuario&gt; itUsuarios = listaUsuarios.iterator();
      while (itUsuarios.hasNext()) {
          if (dniAComprobar.equals(itUsuarios.next().getDni())) {
              if (itUsuarios.next() instanceof UsuarioBiblioteca) {
                  return 1;
              } else if (itUsuarios.next() instanceof Bibliotecario) {
                  return 2;
              } else if (itUsuarios.next() instanceof BibliotecaExterna) {
                  return 3;
              }
          }
      }
      return 0;
  }
  </code></pre>