
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch Elements with Iterator in Java
Let us first create a List and add elements −
List<String>list = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" });
Now, use Iterator to fetch each element −
Iterator<String>i = list.iterator();
Display the elements −
while (i.hasNext()) { System.out.println(i.next()); }
Example
import java.util.Arrays; import java.util.Iterator; import java.util.List; public class Demo { public static void main(String[] a) { List<String>list = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" }); Iterator<String>i = list.iterator(); System.out.println("Displaying elements..."); while (i.hasNext()) { System.out.println(i.next()); } } }
Output
Displaying elements... P Q R S T U V W
Advertisements