List holder = new List();
holder.add(new JTabbedPane());
holder.add(new JTabbedPane());
for(JTabbedPane tab : holder)
{
System.out.println(tab.toString());
}
为什么在 for-each 循环中显示类型不匹配错误?
List holder = new List();
holder.add(new JTabbedPane());
holder.add(new JTabbedPane());
for(JTabbedPane tab : holder)
{
System.out.println(tab.toString());
}
为什么在 for-each 循环中显示类型不匹配错误?
试试这个
ArrayList<JTabbedPane> holder = new ArrayList<JTabbedPane>();
holder.add(new JTabbedPane());
holder.add(new JTabbedPane());
for (JTabbedPane jTabbedPane : holder) {
System.out.println(holder.toString());
}
Try:
for(Object tab : holder)
{
System.out.println( ( (JTabbedPane )tab).toString());
}
它显示错误,因为持有人 is List of Object , not List of JTabbedPane
。所以当你使用 for-each 循环时for(JTabbedPane tab : holder)
,它会显示编译错误。