Flatten Nested List Iterator

Source: Internet
Author: User

1 /**2 *//This is the interface, allows for creating nested lists.3 *//should not implement it, or speculate on its implementation4 * Public interface Nestedinteger {5  *6  *     // @returntrue if this nestedinteger holds a single integer, rather than a nested list.7 * Public boolean isinteger ();8  *9  *     // @returnThe single integer Nestedinteger holds, if it holds a single integerTen *//Return NULL if this nestedinteger holds a nested list One * Public Integer Getinteger (); A  * -  *     // @returnthe nested list that this nestedinteger holds, if it holds a nested list - *//Return NULL if this nestedinteger holds a single integer the * Public list<nestedinteger> getList (); -  * } -  */ -  Public classNestediteratorImplementsIterator<integer> { +     PrivateDeque<integer>Flatten; -      PublicNestediterator (list<nestedinteger>nestedlist) { +Flatten =NewLinkedlist<>(); A Getflatten (nestedlist); at     } -      -     Private voidGetflatten (list<nestedinteger>nestedlist) { -          for(inti = 0; I < nestedlist.size (); i++) { -             if(Nestedlist.get (i). Isinteger ()) { - Flatten.offer (Nestedlist.get (i). Getinteger ()); in}Else { - Getflatten (Nestedlist.get (i). GetList ()); to             } +         } -     } the  * @Override $      PublicInteger Next () {Panax Notoginseng         returnFlatten.pollfirst (); -     } the  + @Override A      Public BooleanHasnext () { the         returnFlatten.size () > 0; +     } - } $  $ /** - * Your Nestediterator object would be instantiated and called as such: - * Nestediterator i = new Nestediterator (nestedlist); the * while (I.hasnext ()) v[f ()] = I.next (); -  */

Flatten Nested List Iterator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.