Iterating Collections and Arrays

Hello Friends,

This is one of tutorials regarding the iterating Collections and Arrays. There are two ways of iterating the elements in the Collections or Arrays. One of the method is using the Iterator and another is using the advanced for loop or foreach statement.  Iterator are used instead of Enumeration in Java and are used to iterate the elements in particular collection. Iterator is mostly used with the collection framework. Whereas advance for loop, introduced in JDK ver. 5, is used with collections and Arrays. It is the enhancement in the for statements, it does not include any type of counters as that of simple for loop.

However, the for-each statement cannot be used in all circumstances. Iterator cannot be used directly with array, so we have to convert the array into a collection, then use the iterator on it for iteration .Following is the code that shows the use of both in Collections and Arrays.

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class PrintArray
{
    public static void main(String args[])
    {
       System.out.println("******Printing Collections******");
       List list1 = new LinkedList();
       list1.add("146");
       list1.add("200");
       list1.add("150");
       list1.add("139");
       System.out.println("using Iterator");
       Iterator iter = list1.iterator();
       while(iter.hasNext())
      {
         Object element = iter.next();
         System.out.print(element+",");
      }
      System.out.println("nusing for-each statement");
      for(Object itrobj : list1)
     {
        System.out.print(itrobj+", ");
     }
     System.out.println("nn******Printing Arrays******");
     String[] arrays = {"viru","sachin","gautam","yuvraj"};
     System.out.println("using for-each statement");
     for(Object obj : arrays)
     {
        System.out.print(obj+", ");
     }
     System.out.println("nusing Iterator");
     List list2 = Arrays.asList(arrays);
     Iterator itr2 = list2.iterator();
     while(itr2.hasNext())
    {
        Object arr = itr2.next();
        System.out.print(arr+",");
    }
  }
}

Hope this will help you.

Thanks,

admin@code2java.com

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.