You want to follow the same pattern as before:
for (Type curInstance: CollectionOf<Type>) {
// use currInstance
}
In this case it would be:
for (Bullet bullet : gunList.get(2).getBullet()) {
System.out.println(bullet);
}
Answer from unholysampler on Stack OverflowHow to do in Java
howtodoinjava.com › home › collections framework › java arraylist › different ways to iterate an arraylist
Different Ways to Iterate an ArrayList
January 12, 2023 - ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles") ); int index = 0; while (namesList.size() > index) { System.out.println(namesList.get(index++)); } Java program to iterate through an ArrayList of objects with Java 8 stream API.
Top answer 1 of 6
47
You want to follow the same pattern as before:
for (Type curInstance: CollectionOf<Type>) {
// use currInstance
}
In this case it would be:
for (Bullet bullet : gunList.get(2).getBullet()) {
System.out.println(bullet);
}
2 of 6
10
Edit:
Well, he edited his post.
If an Object inherits Iterable, you are given the ability to use the for-each loop as such:
for(Object object : objectListVar) {
//code here
}
So in your case, if you wanted to update your Guns and their Bullets:
for(Gun g : guns) {
//invoke any methods of each gun
ArrayList<Bullet> bullets = g.getBullets()
for(Bullet b : bullets) {
System.out.println("X: " + b.getX() + ", Y: " + b.getY());
//update, check for collisions, etc
}
}
First get your third Gun object:
Gun g = gunList.get(2);
Then iterate over the third gun's bullets:
ArrayList<Bullet> bullets = g.getBullets();
for(Bullet b : bullets) {
//necessary code here
}
Videos
08:02
Different Ways to Iterate Over a List (ArrayList) in Java - YouTube
14:52
Java 8 forEach Method Tutorial | Iterate over List, Set, Stream ...
07:09
Java: 6 Ways to Iterate ArrayLists - YouTube
16:37
Different ways of iterating an ArrayList || Important Java Interview ...
GeeksforGeeks
geeksforgeeks.org › java › iterating-arraylists-java
Iterating over ArrayLists in Java - GeeksforGeeks
import java.util.*; class GFG { // Main driver method public static void main(String[] args) { // Creating and initializing the ArrayList // Declaring object of integer type List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); // Iterating using for loop for (int i = 0; i < numbers.size(); i++) // Printing and display the elements in ArrayList System.out.print(numbers.get(i) + " "); } } Output ·
Published January 19, 2026
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-loop-arraylist-in-java
How to loop ArrayList in Java
One of the easiest way to iterate through an ArrayList is by using for loop: import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Chaitanya"); names.add("Rahul"); names.add("Aditya"); // ...
TutorialsPoint
tutorialspoint.com › program-to-iterate-over-a-list-using-java-8-lambda
Program to iterate over a List using Java 8 Lambda
ArrayList<String>list = arrayList; System.out.println("Iterating..."); list.stream().forEach(elem -> System.out.println(elem)); ... import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { ArrayList<String>arrayList = new ArrayList<String>(); ...
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit8-ArrayList › topic-8-3-arraylist-loops.html
8.3. Traversing ArrayLists with Loops — CS Java
So if you are going to add or remove items or you need the index, use a regular for loop or a while loop. ... 8-3-4: Assume that nums has been created as an ArrayList object and it initially contains the following Integer values [0, 0, 4, 2, 5, 0, 3, 0]. What will nums contain as a result of ...
Top answer 1 of 2
3
Simpler way to do it...
ArrayList<MyObject> list = ...
for( MyObject obj : list)
obj.count()
2 of 2
2
You have several ways to do so:
Basic1:
n = 0;
while (n < arrayList.size()) {
(arrayList.get(n)).count();
n++;
}
Basic2:
for (int i = 0; i < arrayList.size(); i++) {
(arrayList.get(i)).count();
}
Better1:
for(ClassTypeOfArrayList item: arrayList) {
item.count();
}
Better2:
Iterator iterator = arrayList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
2 weeks ago - Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.
Programiz
programiz.com › java-programming › examples › iterate-over-arraylist
Java Program to Iterate over an ArrayList
Here, we have used the for-each loop to iterate over the ArrayList and print each element. import java.util.ArrayList; import java.util.ListIterator; class Main { public static void main(String[] args) { // Creating an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(3); numbers.add(2); System.out.println("ArrayList: " + numbers); // Creating an instance of ListIterator ListIterator<Integer> iterate = numbers.listIterator(); System.out.println("Iterating over ArrayList:"); while(iterate.hasNext()) { System.out.print(iterate.next() + ", "); } } }
Codecademy
codecademy.com › docs › java › arraylist › .foreach()
Java | ArrayList | .forEach() | Codecademy
April 13, 2025 - The .forEach() method performs a specified action on each element of the ArrayList one by one. This method is part of the Java Collections Framework and was introduced in Java 8 as part of the Iterable interface, which ArrayList implements.
Java67
java67.com › 2012 › 07 › how-to-iterate-loop-traverse-list-java.html
3 Examples to Loop over a List in Java - ArrayList, LinkedList or Vector | Java67
You can check out The Complete Java Masterclass to learn more about the performance benefits provided by lambda expression and stream in Java 8. Here is a sample Java program that demonstrates How to loop through a List in Java in three different ways, Iterator, for-each loop, and traditional for loop. This technique can be used to loop through ArrayList or any other index-based List implementation like Vector.
TutorialsPoint
tutorialspoint.com › iterate-through-arraylist-in-java
Iterate through ArrayList in Java
July 28, 2025 - In this article, we have learned how to iterate through an ArrayList in Java using different methods such as for loop, while loop, for-each loop, Iterator, and Streams API.
JavaGoal
javagoal.com › home › best way to iterate arraylist in java
Best way to iterate ArrayList in Java - JavaGoal
September 5, 2023 - The enhanced for loop and for-each loop are the same, It’s also another good choice for iterating over collections. When should you use Enhanced for loop for ArrayList
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › java arraylist foreach()
Java ArrayList forEach() with Examples - HowToDoInJava
January 12, 2023 - List<String> list = Arrays.asList("A","B","C","D"); list.forEach(System.out::println); A Consumer implementation takes a single argument, and returns no value. We can also pass the custom actions we have created in other places.
Netjstech
netjstech.com › 2015 › 08 › how-to-loop-iterate-arraylist-in-java.html
How to Loop or Iterate an Arraylist in Java | Tech Tutorials
Java 8 forEach statement to take advantage of parallel computing using · parallel stream. In that case you need to write it like- cityList.parallelStream().forEach((a)->System.out.println("City Name - " + a)); That's all for this topic How to Loop or Iterate an Arraylist in Java.
Codecademy
codecademy.com › docs › java › arraylist › .iterator()
Java | ArrayList | .iterator() | Codecademy
August 13, 2024 - The .iterator() method is used to iterate over the elements of a collection (such as an ArrayList) one by one. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!