返回一个Iterable集合

时间:2014-04-27 19:11:11

标签: java arraylist iterable

目标是从inorder树遍历返回可迭代的集合值。我的inorder遍历方法将所有必要的Positions添加到一个ArrayList(名为'positions')中,然后我循环遍历这个位置列表,访问该位置对象中的值并将它们添加到第二个ArrayList(名为'returns')返回。

我检查了java docs和ArrayList实现了Iterable,所以返回它应该没问题。我收到一个错误,说明两者不兼容。以下是有问题的代码:

/** Returns an iterable collection of the keys of all entries stored in the 
*  map (in inorder). */
  public Iterable<K> keySet() {
        ArrayList<Position<Entry<K,V>>> positions = new ArrayList<Position<Entry<K,V>>>();
        ArrayList<K> returned = new ArrayList<K>();
        if(!isEmpty())
            inorder(root(), positions);
        for(int i = 0; i < positions.size(); i++){
            Position<Entry<K,V>> pos = positions.get(i);
            K key = pos.element().getKey();
            returned.add(key);
        }
        return returned;
  }

这是我得到的错误:

error: incompatible types: ArrayList<K> cannot be converted to Iterable<K>
        return returned;
               ^

0 个答案:

没有答案
相关问题