【问题标题】:Cannot instantiate the type Iterator<ArrayList<Integer>>无法实例化类型 Iterator<ArrayList<Integer>>
【发布时间】:2013-03-19 14:50:29
【问题描述】:
ArrayList<ArrayList<Integer>> wordIndex = new ArrayList<ArrayList<Integer>>(Collections.<ArrayList<Integer>>nCopies(initWord.length(), null)); 

// Populate it.

Iterator<ArrayList<Integer>> iterWordIndex = new Iterator<ArrayList<Integer>>(); 

为什么我不能这样做?

 Cannot instantiate the type Iterator<ArrayList<Integer>>

【问题讨论】:

    标签: java arraylist iterator instantiation


    【解决方案1】:

    您不能实例化Iterator,因为Iterator 是一个接口。您只能实例化具体类。在这种情况下,让ArrayList 为您生成一个:

    Iterator<ArrayList<Integer>> iterWordIndex = wordIndex.iterator();
    

    【讨论】:

      【解决方案2】:

      因为Iterator 是一个接口。您不能创建它的实例。您需要创建实现该接口的具体类的实例(但大多数情况下,您只是从现有集合中获取引用)。

      【讨论】:

        猜你喜欢
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 2014-12-30
        • 2019-07-26
        • 2013-09-27
        • 2015-04-30
        • 1970-01-01
        • 2021-03-12
        相关资源
        最近更新 更多