我想像这样使用以下类:
for(String device : new Devices())
{
//
}
如果我提供对内部字符串数组的直接访问,那么就没有问题:
for(String device : new Devices().getAllDevices()) //getAllDevices would be a String[]
{
//
}
但我只想转发迭代器,如果AllDevices
是ArrayList
.
public final class Devices implements Iterable<String>{
private static final String MyKindleFire = "123156448975312";
private static final String[] AllDevices = new String[]{MyKindleFire};
@Override
public Iterator<String> iterator() {
// if AllDevices were an array list, this would be possible
// but how should I do this for an array?
return AllDevices.iterator();
}
}
这行得通,但如果可能的话,我想知道一个更好的方法:
@Override
public Iterator<String> iterator() {
return Arrays.asList(AllDevices).iterator();
}