Tuesday, March 31, 2020

How To Traverse Iterate Or Loop Arraylist Inwards Java

How to Loop ArrayList inwards Java
Iterating, traversing or Looping ArrayList inwards Java agency accessing every object stored inwards ArrayList together with performing simply about operations similar printing them. There are many ways to iterate, traverse or Loop ArrayList inwards Java e.g. advanced for loop, traditional for loop amongst size(), By using Iterator together with ListIterator along amongst piece loop etc. All the method of Looping List inwards Java too applicable to ArrayList because ArrayList is an essentially List. In side yesteryear side department nosotros volition run across code example of Looping ArrayList inwards Java.



Loop ArrayList inwards Java – Code Example
 inwards Java agency accessing every object stored inwards  How to traverse iterate or loop ArrayList inwards JavaNow nosotros know that at that spot are multiple ways to traverse, iterate or loop ArrayList inwards Java, let’s run across simply about concrete code example to know precisely How to loop ArrayList inwards Java. I prefer advanced for loop added inwards Java 1.5 along amongst Autoboxing, Java Enum, Generics, Varargs together with static import, too known every bit foreach loop if I convey to simply iterate over Array List inwards Java. If I convey to take away elements piece iterating than using Iterator or ListIterator is best solution.



import java.util.ArrayList;
import java.util.Iterator;

/**
 * Java programme which shows How to loop over ArrayList inwards Java using advanced for loop,
 * traditional for loop together with How to iterate ArrayList using Iterator inwards Java
 * payoff of using Iterator for traversing ArrayList is that you lot tin give the axe take away
 * elements from Iterator piece iterating.

 * @author
 */

public class ArrayListLoopExample {

 
    public static void main(String args[]) {
 
        //Creating ArrayList to demonstrate How to loop together with iterate over ArrayList
        ArrayList<String> games = new ArrayList<String>(10);
        games.add("Cricket");
        games.add("Soccer");
        games.add("Hockey");
        games.add("Chess");
     
        System.out.println("original Size of ArrayList : " + games.size());
     
        //Looping over ArrayList inwards Java using advanced for loop
        System.out.println("Looping over ArrayList inwards Java using advanced for loop");
        for(String game: games){
            //print each chemical constituent from ArrayList
            System.out.println(game);
        }
     
        //You tin give the axe too Loop over ArrayList using traditional for loop
        System.out.println("Looping ArrayList inwards Java using uncomplicated for loop");
        for(int i =0; i<games.size(); i++){
            String game = games.get(i);
        }
     
        //Iterating over ArrayList inwards Java
        Iterator<String> itr = games.iterator();
        System.out.println("Iterating  over ArrayList inwards Java using Iterator");
        while(itr.hasNext()){
            System.out.println("removing " + itr.next() + " from ArrayList inwards Java");
            itr.remove();
        }
     
         System.out.println("final Size of ArrayList : " + games.size());
   
    }

}

Output:
master copy Size of ArrayList : 4
Looping over ArrayList inwards Java using advanced for loop
Cricket
Soccer
Hockey
Chess
Looping ArrayList inwards Java using uncomplicated for loop
Iterating  over ArrayList inwards Java using Iterator
removing Cricket from ArrayList inwards Java
removing Soccer from ArrayList inwards Java
removing Hockey from ArrayList inwards Java
removing Chess from ArrayList inwards Java
final Size of ArrayList : 0


That's all on How to iterate, traverse or loop ArrayList inwards Java. In summary role advance for loop to loop over ArrayList inwards Java, its short, construct clean together with fast but if you lot postulate to take away elements piece looping role Iterator to avoid ConcurrentModificationException.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt TreeMap together with TreeSet inwards Java

No comments:

Post a Comment