public String next() { //If our subiterator has some incrementing to //do, then we're off the hook... //Again, if this is the final iterator, then //there is no subiterator so the exclusive //&& is needed. if (!d_voom && subIterator.hasNext()) { return myLetter + subIterator.next(); } //but otherwise, we have to change the //myLetter to the next one in the series //and regenerate the restOfLetters, and thus //even all of the subIterators. Example: "this" //To start, firstLetter is "t" and all sub- //iterators are built on "his". When we //increment firstLetter to "h", we must update //the subiterators to "tis". //Note that restOfLetters was declared INSIDE //the constructor - no matter, we'll need to //rebuild it here anyway. else { //Example: Our string is "computers" and //currentIndex was 3 before calling next(). //Now myLetter becomes "u"... myLetter = jumbledLetters.charAt(currentIndex) + ""; if (!d_voom) { //beforeMyLetter is "comp" String beforeMyLetter = jumbledLetters.substring(0, currentIndex); //afterMyLetter is "ters" String afterMyLetter = jumbledLetters.substring(currentIndex + 1, jumbledLetters.length()); //and restOfLetters is "compters" [sic] //aka, "computers" minus "u". String restOfLetters = beforeMyLetter + afterMyLetter; //Since we're calling the constructor //recursively, all subiterators of this //subiterator are automatically generated. subIterator = new JumbleIterator(restOfLetters); } currentIndex++; //Finally, we'll get our return value. //Don't call next() here, of course, //because we've already iterated just now. return get(); } }