Задача: "The Java Programming Language" Ken Arnold, James Gosling, David Holmes листинги, код, примеры из книги, исходники
Исходник: Глава 16, Коллекции (Chapter 16. Collections), ShortStrings, язык: java [code #168, hits: 6480]
автор: this [добавлен: 11.09.2006]
  1. import java.util.*;
  2.  
  3. /**
  4. * Created by IntelliJ IDEA.
  5. * User: me
  6. * Date: 11.09.2006
  7. * Time: 15:21:14
  8. * To change this template use File | Settings | File Templates.
  9. */
  10. public class ShortStrings implements Iterator {
  11. private Iterator strings;
  12. private String nextShort;
  13. private final int maxLen;
  14.  
  15. public ShortStrings(Iterator strings, int maxLen) {
  16. this.strings = strings;
  17. this.nextShort = null;
  18. this.maxLen = maxLen;
  19. }
  20.  
  21. public boolean hasNext() {
  22. if (nextShort != null)
  23. return true;
  24.  
  25. while (strings.hasNext()) {
  26. nextShort = (String) strings.next();
  27. if (nextShort.length() <= maxLen)
  28. return true;
  29. }
  30. nextShort = null;
  31. return false;
  32. }
  33.  
  34. public Object next() {
  35. if (nextShort == null && !hasNext())
  36.  
  37. String n = nextShort;
  38. nextShort = null;
  39. return n;
  40. }
  41.  
  42. public void remove() {
  43. }
  44. }
Тестировалось на: java 1.5.0_04

+добавить реализацию