Chris
Chris

Reputation: 934

How Do I Iterate Generic List Using For Each Type For Loop

I have been given an assignment to create a list of lists where I should be able to iterate through the lists using the 'for each' type of 'for' loop, versus building a constructor for Iterator. The problem is that when I the code below, I get the error msg "Can only iterate over an array or an instance of java.lang.Iterable". Here is the code:

public static void main(String[] args) {
    GList<InnerList> list = new GList<InnerList>(); 
    GList<InnerList> numList = new GList<InnerList>();
    InnerList lst = new InnerList ();
    Scanner sc = new Scanner(System.in);
    String answer;      
    while (true){
        System.out.println ("Do you want to create a list (y/n)? ");
        answer = sc.next();
        if (answer.equals("y")){
            System.out.println("Enter the name of the list: ");
            answer = sc.next();
            lst.setName(answer);
            if (list.isEmpty()== true){
                list.insertFirstItem(lst);
            }
            else {
                list.insertNext(lst);
            }           
        }               
        while (true){
            System.out.println("Do you want to enter a number (y/n)?");
            answer = sc.next();
            if (answer.equals("y")){
                System.out.println("Enter Number: ");
                answer = sc.next();
                try {
                    int num1 = Integer.parseInt(answer);
                    lst.setInner(num1);
                    if (list.isEmpty() == true){
                        list.insertFirstItem(lst);  
                    }
                    else {
                        list.insertNext(lst);
                    }                       
                }
                catch (NumberFormatException e){
                    System.out.println("You must enter an number! " + e);
                    sc.close();
                }                       
            }
            return;
        }       
    }
    for (GList<InnerList> ilName : list){ //here are my error msgs. I also tried replacing GList<InnerList> with 'InnerList' and String. 
        for(GList<InnerList> ilInts : list){
            System.out.println(ilName);
            System.out.println(ilInts);
        }
    }       
}   

Can someone help me understand why GList is not considered an instance of java.lang.iterable when collections should be iterable?

Thank you.

Upvotes: 2

Views: 5251

Answers (2)

HuntingCheetah
HuntingCheetah

Reputation: 42

It is a syntax error and the coorect syntax would be ,

for(InnerList ilName : GList) {
   for(InnerList ilInts : GList) {
     System.out.println(ilName);
     System.out.println(ilInts);
    }
}  

Hope this helps

Upvotes: -2

JB Nizet
JB Nizet

Reputation: 691715

It's not considered as an instance of java.lang.Iterable because it does not implement the interface java.lang.Iterable. As simple as that. Change its declaration to

public class GList<T> implements Iterable<T>

and you'll be ale to use the foreach loop with it. But since list is an instance of GList<InnerList>, and since a GList<InnerList> contains instances of InnerList (at least, I guess so), the loop should be:

for (InnerList innerList : list) {
    ...
}

Upvotes: 9

Related Questions