Integer foo[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

List<Integer> list = Arrays.asList(foo);
// or
Iterable<Integer> iterable = Arrays.asList(foo);

Though you need to use an Integer array (not an int array) for this to work.

For primitives, you can use guava:

Iterable<Integer> fooBar = Ints.asList(foo);
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>15.0</version>
    <type>jar</type>
</dependency>

For Java8 with lambdas: (Inspired by Jin Kwon's answer)

final int[] arr = { 1, 2, 3 };
final Iterable<Integer> i1 = () -> Arrays.stream(arr).iterator();
final Iterable<Integer> i2 = () -> IntStream.of(arr).iterator();
final Iterable<Integer> i3 = () -> IntStream.of(arr).boxed().iterator();
Answer from fmucar on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_arrays_loop.asp
Java Loop Through an Array
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... 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
🌐
GeeksforGeeks
geeksforgeeks.org › java › iterators-in-java
Iterator in Java - GeeksforGeeks
Iterator is part of the java.util package and is used by collection classes to traverse elements. Collection classes implement the Iterable interface, which provides the iterator() method that returns an Iterator object.
Published   1 month ago
🌐
Oracle
docs.oracle.com › middleware › 12211 › jdev › api-reference-esdk › oracle › javatools › util › ArrayIterator.html
ArrayIterator (Oracle Fusion Middleware Java API Reference for Oracle Extension SDK)
Constructs an ArrayIterator from an array of Objects and an inclusive beginning index. The iterator will return all elements from the beginning index to the end of the array.
🌐
W3Schools
w3schools.com › java › java_iterator.asp
Java Iterator
Java Examples Java Videos Java ... Interview Q&A Java Certificate ... An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet....
🌐
Apache Commons
commons.apache.org › proper › commons-collections › jacoco › org.apache.commons.collections4.iterators › ArrayIterator.java.html
ArrayIterator.java
* </p> * * @param <E> the type of elements returned by this iterator. * @since 1.0 */ public class ArrayIterator<E> implements ResettableIterator<E> { /** The array to iterate over */ final Object array; /** The start index to loop from */ final int startIndex; /** The end index to loop to */ final int endIndex; /** The current iterator index */ int index; /** * Constructs an ArrayIterator that will iterate over the values in the * specified array.
Find elsewhere
🌐
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.
🌐
DataCamp
datacamp.com › doc › java › iterator
Java Iterator
It is part of the Java Collections Framework and is found in the java.util package. The Iterator interface is used to access elements of a collection sequentially without exposing the underlying structure of the collection.
🌐
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++; } } }
🌐
Reddit
reddit.com › r/learnprogramming › i am having a hard time understanding iterators
r/learnprogramming on Reddit: I am having a hard time understanding iterators
August 31, 2021 -

So in an array structure. When you create an array you assign a certain number of memory adress and you use that to store values.

However with iterators, are you just storing objects of a class? For example when you say Basket.add(jo) what exactly are you doing?. Because lets say basket is an iterator. Are you continuasly creating an object of a class similar to Object cat = new Object().

If thats tha case how does this work with arrays? For example in an array list are you continuasly creating new array?

If so isnt that inefficient?

Thank you in advance:)

Top answer
1 of 3
3
Iterators are actually pretty easy. They are just a programmatic way to move through a collections of something. Most languages have a way to tell the compiler that your thing is iterable. The simplest form of an iterator is an interface that has a next() method that will get the next item in the collection. It can return null or some other value if it isn't found. More complex interfaces might have hasNext(), count(), or other methods. You work with iterators all the time in programming. for loops iterate over an iterable collection. forEach constructs rely on iterable collections. Common methods like map(), reduce() and filter() rely on iterable collections. One confusion you have is confusing an iterator with a collection. A collection is just some way of organizing some data. This could be a linked list, an array, a dictionary or some other form of collection. When you call Basket.add(jo), you are adding an item to that collection. What actually happens depends on the data structure you are using. In your example, you ask about arrays. In most languages, arrays are a fixed length and to grow an array, you have to create a new array of a bigger size and copy the values from the old array over. As you said, this can be very expensive. Other collections like lists, linked lists or binary trees don't have this issue. I recommend learning about data structures to help solidify this concept in your mind. The next idea is an iterator. An iterator is a way for your program to access the items in a collection. Most languages let you create your own iterators. There are often built-in iterators for things like arrays. The simplest iterator is a for loop. With a for loop, you are specifying how to iterate over your collection. The classic example is iterating over the members of an array for (i = 0; i <= array.length; i++). In this example, you are iterating over every member of the array (collection). If you modify it to for (i=0; i <= array.length; i = i + 2), then you have a new iterator that accesses every other item in the array (collection). forEach() is a common construct in a lot of languages. Most implementations of forEach() rely on the iterator interface provided by your collection. There are many other methods in languages that rely on an iterator interface. Finally, all iterables are collections, but not all collections are iterable. For example, a common example is dictionaries. In a lot of languages, you cannot use a forEach() construct on a dictionary. Instead, you have to get the collection of keys for the dictionary and iterate over those.
2 of 3
1
Array and ArrayList (a specific kind of Collection) work very differently in Java. When you create an array object you specify the length, whereas a Collection has dynamic size. All classes implementing Collection interface has add, remove and iterator method. Element added to a collection is absorbed as a Node under the hood, an inner class of a Collection. Data is stored there and not with the iterator. Iterator merely is an object of a mechanism to go through all these node objects inside a specific Collection. Arrays on the other hand can be considered as efficient as a primitive type. Under the hood the process of creating an array is deeply optimized by the JVM. So a Collection is not efficient compare to arrays. But when you don’t know how many elements to expect you have to use a Collection. Hope that helps.
🌐
Baeldung
baeldung.com › home › java › java array › convert java array to iterable
Convert Java Array to Iterable | Baeldung
June 27, 2025 - It allows us to iterate over its elements using enhanced for-loops or any other method compatible with Iterables. Another approach to convert an int[] array to an Iterable is using the Stream API’s iterator() method. It allows us to obtain an iterator directly from a stream, offering another convenient option for the conversion process.
🌐
Jenkov
jenkov.com › tutorials › java-collections › iterable.html
Java Iterable
May 25, 2020 - How you implement this Iterable interface so that you can use it with the for-each loop, is explained in the text Implementing the Iterable Interface, in my Java Generics tutorial. Here I will just show you a simple Iterable implementation example though: public class Persons implements Iterable { private List<Person> persons = new ArrayList<Person>(); public Iterator<Person> iterator() { return this.persons.iterator(); } }
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java - How to Convert Java Array to Iterable? - Java Code Geeks
July 6, 2021 - A quick guide to convert an array to iterable in java using Stream api with examples programs in two ways.
🌐
Apache Commons
commons.apache.org › proper › commons-collections › apidocs › org › apache › commons › collections4 › iterators › ArrayIterator.html
ArrayIterator (Apache Commons Collections 4.5.0 API)
java.lang.Object · org.apache... ArrayListIterator · public class ArrayIterator<E> extends Object implements ResettableIterator<E> Implements an Iterator over any array....
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-java-array-to-iterable
How to convert Java Array to Iterable?
import java.util.Arrays; import java.util.Iterator; public class ArrayToIterable { public static void main(String args[]){ Integer[] myArray = {897, 56, 78, 90, 12, 123, 75}; Iterator<Integer> iterator = Arrays.stream(myArray).iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } } } 897 56 78 90 12 123 75 ·
🌐
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
Java developer. · 8y · If you want iterate or traverse collection object one by one , we will use iterator. ... In collection interface there is one method called iterator (), which will give iterator object. ... Software to detect plagiarized code. Find matches for code plagiarism across billions of sources on the web or if it was wrote by ChatGPT. ... What is the difference between foreach and for loops when iterating over arrays and collections in Java?
🌐
Lambdafaq
lambdafaq.org › how-can-i-turn-an-array-into-an-iterator
How can I turn an array into an Iterator? | Maurice Naftalin's Lambda FAQ
Your questions answered: all about Lambdas and friends · If you have an array of reference type, the easiest way is to turn it into a List. Nothing new here:
🌐
Quora
quora.com › Can-you-iterate-through-an-array-in-Java-using-only-one-loop
Can you iterate through an array in Java using only one loop? - Quora
Answer (1 of 2): Yes, you can iterate through an array in Java using only one loop. You can use a [code ]for[/code] loop to achieve this. [code]int[] numbers = {1, 2, 3, 4, 5}; for (int i = 0; i
🌐
Apache Commons
commons.apache.org › proper › commons-collections › javadocs › api-2.1.1 › org › apache › commons › collections › iterators › ArrayIterator.html
ArrayIterator (Collections 2.1.1 release API)
Using this constructor, the iterator is equivalent to an empty iterator until setArray(Object) is called to establish the array to iterate over. public ArrayIterator(java.lang.Object array) Construct an ArrayIterator that will iterate over the values in the specified array.
🌐
Lean Tactic Reference
course.ccs.neu.edu › cs2510sp18 › lecture25.html
Lecture 25: Iterator and Iterable
Moreover, when we phrase it this way, this behavior doesn’t sound very specific to ArrayLists; we might be able to iterate over almost anything! ... The while loop sketch above will work with any object that exposes these two functions as methods. This is a promise to provide a certain set of behaviors, so we should accordingly define a new interface. This interface is called an Iterator, and it is provided for us by Java itself.