Skip to content

Instantly share code, notes, and snippets.

@Rohit-554
Created March 25, 2023 18:35
Show Gist options
  • Select an option

  • Save Rohit-554/6cf9e7c9d39c24b564e66814278e1277 to your computer and use it in GitHub Desktop.

Select an option

Save Rohit-554/6cf9e7c9d39c24b564e66814278e1277 to your computer and use it in GitHub Desktop.
iterator_Iterator
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