using System; using System.Collections; class Program { static void Main(string[] args) { ArrayList numbers = new ArrayList(); numbers.Add(1); numbers.Add(2); numbers.Add(3); IEnumerator iterator = numbers.GetEnumerator(); while (iterator.MoveNext()) { Console.WriteLine(iterator.Current); } } }
import java.util.ArrayList; import java.util.Iterator; public class Main { public static void main(String[] args) { ArrayListIn this example, an ArrayList is created and filled with three integers. An Iterator is created using the iterator() method, and a while loop is used to iterate over each element in the ArrayList. The next() method is used to retrieve the current element in the iteration. Both examples use the iterator pattern to navigate through a collection of objects. The C# implementation is part of the System.Collections namespace, while the Java implementation is part of the java.util package.numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); Iterator iterator = numbers.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }