简体   繁体   中英

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.

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) {
    ...
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM