Created
March 25, 2023 18:35
-
-
Save Rohit-554/6cf9e7c9d39c24b564e66814278e1277 to your computer and use it in GitHub Desktop.
iterator_Iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Iterable_iterator implements Iterable<Integer> { | |
| private List<Integer> myList; | |
| public Iterable_iterator(List<Integer> myList) { | |
| this.myList = myList; | |
| } | |
| @Override | |
| public Iterator<Integer> iterator() { | |
| return myList.iterator(); | |
| } | |
| public static void main(String[] args) { | |
| List<Integer> myList = new ArrayList<Integer>(); //we have created a list myList | |
| myList.add(1); //adding the value to it | |
| myList.add(2); | |
| myList.add(3); //123 | |
| Iterable_iterator myIterable = new Iterable_iterator(myList); //we create object of the class | |
| Iterator<Integer> iterator = myIterable.iterator(); | |
| while (iterator.hasNext()) { //elements exist | |
| Integer i = iterator.next(); // accessing the next element | |
| System.out.println(i); //printing | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment