Advertisement
techno-

Circular

Nov 4th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. package e2;
  2.  
  3. import java.util.Iterator;
  4. import java.util.List;
  5.  
  6.  
  7. public class Circular implements Iterator<String>{
  8.     private List<String> lista;
  9.     private int k;
  10.     private int pos= 0;
  11.     private int poscorrecta;
  12.  
  13.     public Circular(List<String> lista, int k){
  14.         this.lista=lista;
  15.         this.k=k;
  16.     }
  17.  
  18.  
  19.     @Override
  20.     public boolean hasNext() {
  21.         if (lista.size()>1){
  22.             return true;
  23.         } else return false;
  24.     }
  25.  
  26.     @Override
  27.     public String next() {
  28.         pos=pos+k-1;
  29.         poscorrecta = pos % lista.size();
  30.         System.out.println("la pos es " + (poscorrecta+1));
  31.         String resultado = lista.get(poscorrecta);
  32.  
  33.         return resultado;
  34.     }
  35.  
  36.     @Override
  37.     public void remove() {
  38.         lista.remove(poscorrecta);
  39.         if(pos> lista.size()){
  40.             pos=0;
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement