How to Loop Through an ArrayList in Java


In this example we will show the method to loop through an ArrayList with for-each in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;

public class LoopAnArrayList { 
  public static void main(String[] args) { 
    ArrayList strs = new ArrayList();
    strs.add("a");
    strs.add("b");
    strs.add("c");
    for (String s : strs) { // loop through an ArrayList
      System.out.println(s);
    }
  } 
}

Output:

a
b
c

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

How to Loop Through an ArrayList in Java


In this example we will show the method to loop through an ArrayList in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;

public class LoopArrayList {
  public static void main(String[] args) { 
    ArrayList strs = new ArrayList();
    strs.add("a");
    strs.add("b");
    strs.add("c");
    for (int i = 0; i < strs.size(); i++) { // loop through an ArrayList
      System.out.println(strs.get(i));
    }
  }
}

Output:

a
b
c

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments