我有一个对象o
,我知道它是一个数组。
如何迭代ů
?
Object o;
if (o != null && o.getClass().isArray()) {
for (Object each : o) { // compile error, of course
System.out.println(each);
}
}
最佳答案
将其转换为数组,如下所示:
public static void main(String... args) {
Object o = new Object[]{"one", "two"};
if (o != null && o.getClass().isArray()) {
for (Object each : (Object[])o) { // no compile error, of course
System.out.println(each);
}
}
}
关于java - 迭代对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23292907/