Solve the Can Only Iterate Over an Array or an Instance of Java.Lang.Iterable Error in Java

Haider Ali Jan 30, 2023 Aug 26, 2022
  1. Can Only Iterate Over an Array or an Instance of java.lang.iterable
  2. Solve the Can Only Iterate Over an Array or an Instance of java.lang.iterable Error Using Iterator()
Solve the Can Only Iterate Over an Array or an Instance of Java.Lang.Iterable Error in Java

We will learn about the Java error Can only iterate over an array or an instance of java.lang.iterable. We will see why this error occurs and the solution to it.

So without any further delay, let’s dive in.

Can Only Iterate Over an Array or an Instance of java.lang.iterable

This error is a compile time error, and it’s just as it says. It occurs when there is a problem with the iteration of an array or an instance.

While programming, the user tries to make things easier for themselves, and while doing so, the user uses loops. However, using loops is not always the correct answer.

The error can only iterate over an array or an instance of java.lang.iterable doesn’t mean that it stops the user from using a loop on an array or an instance. It means that a loop is used that doesn’t complement its conditions - for example, the for or foreach loop.

Solve the Can Only Iterate Over an Array or an Instance of java.lang.iterable Error Using Iterator()

In the case of loops, if a foreach loop is used, we have to explicitly type our iterations as sometimes foreach can cause this error to occur. We can do that by using Iterator.

Another way is to use a simple for or while loop.

Here is a simple example of explicitly iterating an array using an Iterator with a while loop. Here we will use ArrayList to demonstrate Iterator().

An ArrayList is used here because Iterator() is a method of the ArrayList class.

A while loop is used here to make things easier. This is because while using other loops, for example, for and foreach, the Iterator() method does not work correctly.

Since Iterator() is part of a collection method, it works properly with specific loops, like the while loop.

Code:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

public class Main
{
    public static void main(String[] args)
    {
        List<Integer> Num = new ArrayList<>(); //ArrayList is used here
        Num.add(1);
        Num.add(2);
        Num.add(3);
        Num.add(4);

        Iterator<Integer> value = Num.iterator(); //Here is the use of Iterator()
        while (value.hasNext()) //hasNext() is used to loop. It is a method of Iterator()
        {
            System.out.println(value.next());
        }
    }
}

Output:

1
2
3
4
Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Java Error