W3Schools
w3schools.com โบ java โบ java_howto_loop_through_arraylist.asp
Java How To Loop Through an ArrayList
For Loop Nested Loops For-Each Loop Real-Life Examples Code Challenge Java Break/Continue Java Arrays
W3Schools
w3schools.com โบ java โบ java_arrays_loop.asp
Java Loop Through an Array
You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.
Videos
Java Program to Iterate an ArrayList | Iterate over an ArrayList ...
09:47
How To Loop Through An Array List In Java | Java Full Course From ...
02:58
Java ArrayList in for-Loop Tutorial | Filling Up an ArrayList - ...
08:02
Different Ways to Iterate Over a List (ArrayList) in Java - YouTube
04:22
Using a For-each Loop to Traverse an ArrayList - YouTube
07:09
Java: 6 Ways to Iterate ArrayLists - YouTube
W3Schools
w3schools.com โบ java โบ ref_arraylist_foreach.asp
Java ArrayList forEach() Method
For Loop Nested Loops For-Each Loop Real-Life Examples Code Challenge Java Break/Continue Java Arrays
W3Schools
w3schools.com โบ java โบ java_arraylist.asp
Java ArrayList
GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum ... assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof ...
Runestone Academy
runestone.academy โบ ns โบ books โบ published โบ csawesome โบ Unit7-ArrayList โบ topic-7-3-arraylist-loops.html
7.3. Traversing ArrayLists with Loops โ CSAwesome v1
You can also use a while loop or ... of using the index operator [] to access elements, you use the get(index) method to get the value at the index and set(index,value) to set the element at an index to a new value....
W3Schools
w3schools.com โบ java โบ java_foreach_loop.asp
Java For-Each Loop
The for-each loop is simpler and more readable than a regular for loop, since you don't need a counter (like i < array.length). The following example prints all elements in the cars array: String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (String car : cars) { System.out.println(car); } ... Here is a similar example with numbers.
W3Schools
w3schools.com โบ java โบ java_ref_arraylist.asp
Java ArrayList Reference
For Loop Nested Loops For-Each Loop Real-Life Examples Code Challenge Java Break/Continue Java Arrays
W3Schools
w3schools.com โบ java โบ java_for_loop.asp
Java For Loop
How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
W3Schools
w3schools.com โบ java โบ ref_arraylist_iterator.asp
Java ArrayList iterator() Method
GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum ... assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof ...
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) + " "); } }
Published ย January 19, 2026
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.
W3Schools
w3schools.com โบ java โบ ref_arraylist_toarray.asp
Java ArrayList toArray() Method
GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum ... assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof ...
W3Schools
w3schools.com โบ cs โบ cs_arrays_loop.php
C# Loop Through an Array
Classes and Objects Multiple Objects ... C# Files C# Exceptions ... You can loop through the array elements with the for loop, and use the Length property to specify how many times the loop should run....
W3Schools
w3schools.com โบ java โบ java_iterator.asp
W3Schools.com
GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum ... assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof ...
W3Schools
w3schools.com โบ java โบ ref_arraylist_get.asp
Java ArrayList get() Method
GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum ... assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof ...
Runestone Academy
runestone.academy โบ ns โบ books โบ published โบ csjava โบ Unit8-ArrayList โบ topic-8-3-arraylist-loops.html
8.3. Traversing ArrayLists with Loops โ CS Java
You can also use a while or for loop to process list elements using the index. The ArrayList index starts at 0 just like arrays, but instead of using the square brackets [] to access elements, you use the get(index) to get the value at the index and set(index,value) to set the element at an ...
Top answer 1 of 4
14
I think fundamentally the code is correct. I would check your inputs and make sure they're really what you think.
I would perhaps rewrite your loop as:
for (String s : contain) {
if (s.contains(code)) {
// found it
}
}
to make use of the object iterators (the above assumes you have an ArrayList<String>). And perhaps rename contain. It's not very clear what this is.
2 of 4
11
The code is correct assuming List of strings. I have not modified any of your source code just to give you idea that it works fine.
List<String> contain = new ArrayList<String>();
contain.add("HPDH-1,001, Check-out date: 7/7/7");
contain.add("JTI-1,001, Check-out date: 7/7/7");
String code = "JTI-1 ";
for (int i = 0; i < contain.size(); i++) {
if (contain.get(i).contains(code.trim())) {<---Use trim it is possible that code may have extra space
System.out.println(contain.get(i));
}
}
BeginnersBook
beginnersbook.com โบ 2013 โบ 12 โบ how-to-loop-arraylist-in-java
How to loop ArrayList in Java
You can also loop through an ArrayList elements using stream. 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"); names.stream().fo...
W3Schools
w3schools.com โบ java โบ java_challenges_for_loop.asp
Java For Loop Code Challenge
How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum