【问题标题】:How Do I Iterate Generic List Using For Each Type For Loop如何使用 For 循环的每种类型迭代通用列表
【发布时间】:2013-02-23 17:01:39
【问题描述】:

我被分配创建一个列表列表,我应该能够使用“for each”类型的“for”循环遍历列表,而不是为迭代器构建一个构造函数。问题是,当我编写下面的代码时,我收到错误消息“只能遍历数组或 java.lang.Iterable 的实例”。代码如下:

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);
        }
    }       
}   

谁能帮我理解为什么当集合应该是可迭代的时 GList 不被视为 java.lang.iterable 的实例?

谢谢。

【问题讨论】:

  • 显示什么错误??
  • 原帖中的错误是""Can only iterate over an array or an instance of java.lang.Iterable"

标签: java collections foreach generic-list


【解决方案1】:

它不被视为java.lang.Iterable 的实例,因为它没有实现接口java.lang.Iterable。就如此容易。将其声明更改为

public class GList<T> implements Iterable<T>

然后你就可以使用 foreach 循环了。但是由于listGList&lt;InnerList&gt; 的实例,并且由于GList&lt;InnerList&gt; 包含InnerList 的实例(至少,我猜是这样),所以循环应该是:

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

【讨论】:

  • 你是对的,它确实包含 InnerList 的实例。当我将 'implements Iterable' 添加到 GList 类时,它告诉我“类型 GList 必须实现继承的抽象方法 Iterable.iterator()”;这是什么意思?
  • 必须实现Iterable接口中声明的方法:public Iterator&lt;T&gt; iterator()。你了解接口的概念吗?如果是这样,您应该了解在您实现的接口中声明的每个方法都必须实现。如果没有,请阅读一本好的 Java 入门书籍,或 the Java tutorial
  • 我理解接口的概念,但我们被赋予了我们被允许使用的“唯一”公共方法,而 public Iterator 不是其中之一?
  • 如果 GList 没有实现 Iterable,则不能使用 foreach 循环对其进行迭代。没有解决方法。所以问问你的老师他真正想要什么。
  • 我有一个 public boolean hasNext(T head) 方法?如果最好的话,我可以添加其余的...
【解决方案2】:

这是一个语法错误,正确的语法是,

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

希望对你有帮助

【讨论】:

  • 这实际上是错误的。这不是使用增强型的正确方法。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
相关资源
最近更新 更多