If you're looping through an array, it shouldn't matter - the enhanced for loop uses array accesses anyway.

For example, consider this code:

public static void main(String[] args)
{
    for (String x : args)
    {
        System.out.println(x);
    }
}

When decompiled with javap -c Test we get (for the main method):

public static void main(java.lang.String[]);
  Code:
   0:   aload_0
   1:   astore_1
   2:   aload_1
   3:   arraylength
   4:   istore_2
   5:   iconst_0
   6:   istore_3
   7:   iload_3
   8:   iload_2
   9:   if_icmpge   31
   12:  aload_1
   13:  iload_3
   14:  aaload
   15:  astore  4
   17:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   20:  aload   4
   22:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   25:  iinc    3, 1
   28:  goto    7
   31:  return

Now change it to use an explicit array access:

public static void main(String[] args)
{
    for (int i = 0; i < args.length; i++)
    {
        System.out.println(args[i]);
    }
}

This decompiles to:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   aload_0
   4:   arraylength
   5:   if_icmpge   23
   8:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   11:  aload_0
   12:  iload_1
   13:  aaload
   14:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   17:  iinc    1, 1
   20:  goto    2
   23:  return

There's a bit more setup code in the enhanced for loop, but they're basically doing the same thing. No iterators are involved. Furthermore, I'd expect them to get JITted to even more similar code.

Suggestion: if you really think it might make a significant difference (which it would only ever do if the body of the loop is absolutely miniscule) then you should benchmark it with your real application. That's the only situation which matters.

Answer from Jon Skeet on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays_loop.asp
Java Loop Through an Array
Arrays Loop Through an Array Real-Life Examples Multidimensional Arrays Code Challenge ยท Java Methods Java Method Challenge Java Method Parameters
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ iterating-arrays-java
Java - Loop Through an Array - GeeksforGeeks
December 2, 2024 - Example 3: We can also loop through an array using while loop. Although there is not much of a difference in for and while loop in this condition. While loop is considered effective when the increment of the value depends upon some of the condition. ... // Java program to iterate over an array // using while loop import java.io.*; class Main { public static void main(String args[]) { // taking an array int a[] = { 1, 2, 3, 4, 5 }; int i = 0; // Iterating over an array // using while loop while (i < a.length) { // accessing each element of array System.out.print(a[i] + " "); i++; } } }
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 381420 โ€บ java โ€บ loop-arraylist-arrays
loop through arraylist of arrays (Java in General forum at Coderanch)
November 13, 2006 - I get as far as, for example: details - arraylist of object Details that has a field named Data, which is also an arraylist. Data stored arrays in the arraylist. count - an outer loop i'm using to iterate through all details arraylists. for (int i=0; i < details.get(count-1).getData().size(); i++){ this is as far as i get.
๐ŸŒ
Quora
quora.com โ€บ What-is-iteration-What-does-it-mean-to-iterate-through-an-array-in-Java
What is iteration? What does it mean to iterate through an array in Java? - Quora
That said, your question tells ... to me: In its simplest terms, to iterate over something is to access each element of a collection, one by one....
Top answer
1 of 6
69

If you're looping through an array, it shouldn't matter - the enhanced for loop uses array accesses anyway.

For example, consider this code:

public static void main(String[] args)
{
    for (String x : args)
    {
        System.out.println(x);
    }
}

When decompiled with javap -c Test we get (for the main method):

public static void main(java.lang.String[]);
  Code:
   0:   aload_0
   1:   astore_1
   2:   aload_1
   3:   arraylength
   4:   istore_2
   5:   iconst_0
   6:   istore_3
   7:   iload_3
   8:   iload_2
   9:   if_icmpge   31
   12:  aload_1
   13:  iload_3
   14:  aaload
   15:  astore  4
   17:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   20:  aload   4
   22:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   25:  iinc    3, 1
   28:  goto    7
   31:  return

Now change it to use an explicit array access:

public static void main(String[] args)
{
    for (int i = 0; i < args.length; i++)
    {
        System.out.println(args[i]);
    }
}

This decompiles to:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   aload_0
   4:   arraylength
   5:   if_icmpge   23
   8:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   11:  aload_0
   12:  iload_1
   13:  aaload
   14:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   17:  iinc    1, 1
   20:  goto    2
   23:  return

There's a bit more setup code in the enhanced for loop, but they're basically doing the same thing. No iterators are involved. Furthermore, I'd expect them to get JITted to even more similar code.

Suggestion: if you really think it might make a significant difference (which it would only ever do if the body of the loop is absolutely miniscule) then you should benchmark it with your real application. That's the only situation which matters.

2 of 6
22

This falls squarely in the arena of micro-optimization. It really doesn't matter. Stylistically I always prefer the second because it's more concise, unless you need the loop counter for something else. And that's far more important than this kind of micro-optimization: readability.

That being said, For an ArrayList there won't be much difference but a LinkedList will be much more efficient with the second.

๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ csjava โ€บ Unit7-Arrays โ€บ topic-7-2-traversing-arrays.html
7.2. Traversing Arrays with For Loops โ€” CS Java
First trace through it on paper keeping track of the array and the index variable. Then, run it to see if you were right. You can also follow it in the visualizer by clicking on the Show Code Lens button. We can use iteration with a for loop to visit each element of an array.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ java-and-array-iteration-what-beginners-need-to-know-22a44dd32afb
Java and Array Iteration โ€” What Beginners Need to Know
June 17, 2024 - Learn the basics of array iteration in Java, exploring methods including for loops, while loops, for-each loops, and advanced techniques using streams.
Find elsewhere
๐ŸŒ
Zero To Mastery
zerotomastery.io โ€บ blog โ€บ enhanced-for-loop-java
How To Use Enhanced For Loops In Java (aka 'foreach') | Zero To Mastery
January 26, 2024 - I know we covered quite a few things here, so hopefully you managed to grasp what we've been talking about. ... The enhanced for loop (also known as a foreach) loop in Java offers a simplified, error-resistant way to iterate over arrays and ...
๐ŸŒ
DEV Community
dev.to โ€บ jacobisah โ€บ java-iteration-tutorial-how-to-loop-through-an-array-in-java-3ad
Java Iteration Tutorial: How to Loop Through an Array in Java - DEV Community
August 15, 2022 - The condition is verified both at startup and at the beginning of each iteration. If this condition is met, the loop body is performed. If it is false, the loop is terminated and the program control is transferred to the line after the loop. To loop through arrayelements, the for-each loop is utilized.
๐ŸŒ
CodingBat
codingbat.com โ€บ doc โ€บ java-array-loops.html
CodingBat Java Arrays and Loops
In contrast, Java Lists can grow and shrink over time -- this is a big feature that Lists have that arrays do not. The length of an array can be accessed as a special ".length" attribute. For example, with the above "values" array, we can access the size of the array as "values.length". It is common to use a 0...length-1 for-loop to iterate over all the elements in array:
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-are-the-different-ways-to-iterate-over-an-array-in-java
What are the different ways to iterate over an array in Java?
July 2, 2020 - import java.util.Arrays; public class IteratingArray { public static void main(String args[]) { //Creating an array int myArray[] = new int[7]; //Populating the array myArray[0] = 1254; myArray[1] = 1458; myArray[2] = 5687; myArray[3] = 1457; myArray[4] = 4554; myArray[5] = 5445; myArray[6] = 7524; //Printing Contents using for each loop System.out.println("Contents of the array: "); for (int element: myArray) { System.out.println(element); } } }
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ java โ€บ nutsandbolts โ€บ arrays.html
Arrays (The Javaโ„ข Tutorials > Learning the Java Language > Language Basics)
In a real-world programming situation, you would probably use one of the supported looping constructs to iterate through each element of the array, rather than write each line individually as in the preceding example. However, the example clearly illustrates the array syntax.
๐ŸŒ
Instructables
instructables.com โ€บ design โ€บ software
How to Use a While Loop to Iterate an Array in Java : 9 Steps - Instructables
July 22, 2022 - How to Use a While Loop to Iterate an Array in Java: Today I will be showing you how to use Java to create a While loop that can be used to iterate through a list of numbers or words. This concept is for entry-level programmers and anyone who wants to get a quick brush-up on Java Loops and arrays.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 02 โ€บ how-to-loop-through-array-in-java-with.html
How to loop through an Array in Java? Example Tutorial
The traditional loop uses a counter and allows you to iterate until the last element is reached i.e. counter is equal to the length of the array while enhanced for loop maintains that counter internally, allowing you to iterate without worrying about counts. This results in clean code and also eliminates the possibility of one-off errors. In this article, I'll show you 2 ways to iterate over an array, first by using traditional for loop and second by using enhanced for loop of Java 1.5.
Top answer
1 of 3
6

The Java Collections framework is something that you should browse. There are also some types that aren't array-like (Set and Map) but you will find that you will use them often. There are many different types of collections, and some have advantages over others for certain types of operations.

Classic arrays

First, the thing that isn't in the collections framework. The Array. Not ArrayList, but rather the classic int[]. The advantage of the array is that it closely maps to what memory is. foo[42] is to lookup. Many system level calls that give you a fixed number of items are arrays and they are good to know. "1,2,3".split(",") returns back an array for example.

The problem with arrays is that they aren't that flexible. Adding an element to the end of an array that already is at is maximum capacity means allocating a new, larger array and then doing an System.arraycopy() (javadoc) to copy all the elements from one array to another array in a timely manner.

To remove an element from the list you need to remove it and then walk the remainder of the list to have everything be contagious again. If you want to remove foo[4] from the list, you then assign foo[4] = foo[5] and foo[5] = foo[6] and so on down to the end of the list. Thats if you have to do it by hand. You can also use that System.arraycopy mentioned to do it for you given the right set of arguments. Something like:

System.arraycopy(foo,5,foo,4,foo.length - 4);

I might have that wrong, but thats the general idea. Copying the array back on itself. It should work.

If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.

Lists

As you see, arrays are a bit awkward to work with. You've got do it all by hand. Its good to know, but a pain to practice. There are two lists that are in common use that make this easier for you. For the most part, Lists look like arrays.

List<Integer> foo = ...;
foo.get(4);
foo.remove(5);

You call methods on them, but you are fetching the 4th element via method call instead of via direct access through the array. See that foo.remove(5). Thats it. You're done. You have removed an element from the list and deleted it. Much easier to remember.

ArrayList

The ArrayList is backed by an Array. This means that doing foo.get(4) runs fast, but foo.add(42, foo.size()+1) is likely to be slow because it has to do all of that moving of things around to allocate a new list (yes, I know that the list can be longer than the size, but lets assume that its the same size as its actual list).

Instead of having to remember how to do all the shuffling, you've got nice methods to do your work for you. It doesn't mean that its not done (the work is still done behind the scenes of the interface), but its something that you don't have to worry about.

So, the ArrayList is for:

  • fast get (O(1))
  • slow remove and insert (O(length))
  • slow append when full (O(length))

LinkedList

For some reason, everyone's goto list implementation is the ArrayList. I don't know why. Many times when I'm working with Lists, the LinkedList is the one that is more applicable. You keep adding things to the end. Rarely do you want the nth element. You just want to iterate over them.

This is what the LinkedList is good for. It does fast remove and append and its iterator is just as fast as the ArrayList. Its just if you want foo.get(400) its going to be a bit slower than the ArrayList implement.

The LinkedList is also a Deque too, but people forget about that. That lets you access the LinkedList as a double ended queue (nice for implementing a Stack or Queue).

So, if you are just appending and iterating, the LinkedList is for you.

  • Get is slowish (O(index))
  • Fast append (O(1))
  • Remove and insert is slowish (O(index))

Note that the O(index) there is a bit of an award bit. It will index depending on which way is faster to get there (from the end or start). So if the LinkedList is 100 long, and you get(10), it starts from the start. If you ask for get(90), it starts from the end. So get(index) is never more than get(length/2), but thats all just factors that don't matter when looking at Big O.

2 of 3
1

If you must really do this using an array, then you will need to:

  • copy all elements of the array after the element being deleted down by one position to cover the removed element.

  • resize your array to be smaller by one element.

Copying elements down by one position can be done either by writing a loop yourself, or by invoking System.arraycopy( src, srcPos, dest, destPos, length).

Resizing the array can be done like this: myArray = Arrays.copyOf( myArray, newCapacity ).

That having been said, if you have any need to manipulate the length of the array, then definitely go with ArrayList instead of using arrays. This wheel has already been invented.

๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array
Array - JavaScript | MDN
February 24, 2026 - The array argument passed to callbackFn is most useful if you want to read another index during iteration, because you may not always have an existing variable that refers to the current array. You should generally not mutate the array during iteration (see mutating initial array in iterative methods), but you can also use this argument to do so.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-iterate-array-in-reverse-order
Java - Iterate Array in Reverse Order - GeeksforGeeks
July 23, 2025 - In Java, iterating over an array in reverse order means accessing the elements of the array from the last to the first.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ iterate-through-list-in-java
Iterate through List in Java - GeeksforGeeks
July 23, 2025 - // Java Program to iterate over List // Using simple for loop // Importing all classes of // java.util package import java.util.*; // CLass class GFG { // Main driver method public static void main(String args[]) { // Creating a ArrayList List<String> myList = new ArrayList<String>(); // Adding elements to the list // Custom inputs myList.add("A"); myList.add("B"); myList.add("C"); myList.add("D"); // For loop for iterating over the List for (int i = 0; i < myList.size(); i++) { // Print all elements of List System.out.println(myList.get(i)); } } } Output ยท