java - Custom written Iterator for a Set throws Exception in for-each loop -


i'm trying write custom made iterator set made. i'm bit confused contract interface iterable. has 3 methods: next(), hasnext() , remove(). set immutable i'm planning on throwing unsupportedoperationexception method remove(). called "lazily generated" ie elements not stored in memory rather created when needed, that's neither here nor there.

the javadoc iterator's next() method follows:

e next()  returns next element in iteration. returns: next element in iteration throws: nosuchelementexception - if iteration has no more elements 

and hasnext() is:

boolean hasnext()  returns true if iteration has more elements. (in other words, returns  true if next() return element rather throwing exception.) 

going these rules, started implementing set , iterator, getting this:

import java.util.abstractset; import java.util.iterator;  public class primesbelow extends abstractset<integer>{      int max;     int size;      public primesbelow(int max) {         this.max = max;     }      @override     public iterator<integer> iterator() {         return new setiterator<integer>(this);     }      @override     public int size() {         if(this.size == -1){             system.out.println("calculating size");             size = calculatesize();         }else{             system.out.println("accessing calculated size");         }         return size;     }      private int calculatesize() {         int c = 0;         for(integer p: this)             c++;         return c;     }      public static void main(string[] args){         primesbelow primesbelow10 = new primesbelow(10);         for(int i: primesbelow10)             system.out.println(i);         system.out.println(primesbelow10);     } } 

.

import java.util.iterator; import java.util.nosuchelementexception;  public class setiterator<t> implements iterator<integer> {     int max;     int current;     public setiterator(primesbelow pb) {         this.max= pb.max;         current = 1;     }      @override     public boolean hasnext() {         if(current < max) return true;         else return false;     }      @override     public integer next() {         while(hasnext()){             current++;             if(isprime(current)){                 system.out.println("returning "+current);                 return current;             }         }         throw new nosuchelementexception();     }      private boolean isprime(int a) {         if(a<2) return false;         for(int = 2; < a; i++) if((a%i)==0) return false;         return true;     } } 

this seemed fine my, next() return next value , throws exception when there no more. hasnext() should return true if there more values iterate on , false otherwise. output main loop this:

returning 2 2 returning 3 3 returning 5 5 returning 7 7 exception in thread "main" java.util.nosuchelementexception     @ setiterator.next(setiterator.java:27)     @ setiterator.next(setiterator.java:1)     @ primesbelow.main(primesbelow.java:38) 

so seems exception isn't handled in each loop. how write custom iterator can use works? tried returning null instead of throwing exception gives nullpointerexception.

should return null when iterator finished, or throw exception? javadoc says next() should throw exception when hover on overriden method next() in eclipse signature doesn't show throws nosuchelementexception i'm confused contract says. seems weird me return null when i'm done since collection potentially contain null elements.

thank help.

change

while(hasnext()) {     //... } throw new nosuchelementexception(); 

to

if(hasnext()) {     //... } else {     throw new nosuchelementexception(); } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -