for-each 循环只能遍历数组或 java.lang.Iterable 的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18433962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
for-each loop can only iterate over an array or an instance of java.lang.Iterable
提问by turtlesoup
The following code doesn't work. What's wrong with this code? Compiler complains in the for loop that NumberList
isn't a Iterable
class.
以下代码不起作用。这段代码有什么问题?编译器在NumberList
不是Iterable
类的 for 循环中抱怨。
What kind of class can be used in for-each loop? How to make NumberList
iterable? I tried making NumberList implement Iterable
, but it doesn't seem to work because I don't know how to define the Iterator properly.
在 for-each 循环中可以使用什么样的类?如何使NumberList
可迭代?我试过 make NumberList implement Iterable
,但它似乎不起作用,因为我不知道如何正确定义 Iterator。
If someone could demonstrate how to make this code work, or link me to a tutorial that'd be great.
如果有人可以演示如何使此代码工作,或者将我链接到一个很棒的教程。
public class Test{
public class NumberList{
private int numItems;
private Number[] numbers;
public NumberList(int size){
this.numbers = new Number[size];
this.numItems=0;
}
public void add(Number n){
this.numbers[this.numItems++]=n;
}
}
public void printPairs() {
ArrayList<Integer> num=new ArrayList<Integer>();
NumberList numbers = new NumberList(50);
numbers.add(4);
numbers.add(5);
numbers.add(6);
for(Number n1: numbers){
System.out.println(n1);
}
}
}
采纳答案by monkHyman
NumberList does not implement Iterable. As far as the compiler is concerned its just any other class.
NumberList 没有实现 Iterable。就编译器而言,它只是任何其他类。
You need to do something like this
你需要做这样的事情
public class NumberList implements Iterable<Number> {
private int numItems;
private Number[] numbers;
public NumberList(int size) {
this.numbers = new Number[size];
this.numItems = 0;
}
public void add(Number n) {
this.numbers[this.numItems++] = n;
}
@Override
public Iterator<Number> iterator() {
return Arrays.asList(numbers).subList(0, numItems).iterator();
}
}
回答by DaoWen
You need to implementthe Iterable interface. In this case, that just means you need to add a method Iterator<T> iterator()
to your NumberList
class. Since your list only contains numbers in this case, the generic type parameter T
is just Number
.
你需要实现的可迭代接口。在这种情况下,你需要添加一个方法只是意味着Iterator<T> iterator()
你的NumberList
类。由于您的列表在这种情况下仅包含数字,因此泛型类型参数T
只是Number
.
回答by amatellanes
Your class NumberList
need to implement the Iterableinterface:
你的类NumberList
需要实现Iterable接口:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class Test {
public class NumberList implements Iterable<Number> {
private int numItems;
private Number[] numbers;
public NumberList(int size) {
this.numbers = new Number[size];
this.numItems = 0;
}
public void add(Number n) {
this.numbers[this.numItems++] = n;
}
@Override
public Iterator<Number> iterator() {
return Arrays.asList(numbers).iterator();
}
}
public void printPairs() {
ArrayList<Integer> num = new ArrayList<Integer>();
NumberList numbers = new NumberList(50);
numbers.add(4);
numbers.add(5);
numbers.add(6);
for (Number n1 : numbers) {
System.out.println(n1);
}
}
}