0
List holder = new List();
holder.add(new JTabbedPane());
holder.add(new JTabbedPane());

for(JTabbedPane tab : holder)
{
    System.out.println(tab.toString());
}

为什么在 for-each 循环中显示类型不匹配错误?

4

2 回答 2

5

试试这个

ArrayList<JTabbedPane> holder = new ArrayList<JTabbedPane>();
        holder.add(new JTabbedPane());
        holder.add(new JTabbedPane());
        for (JTabbedPane jTabbedPane : holder) {
            System.out.println(holder.toString());
        }
于 2013-01-22T10:25:01.590 回答
0
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),它会显示编译错误。

于 2013-01-22T10:29:35.567 回答