Java:迭代通用HashSet <e>的类型不匹配错误

时间:2016-11-17 04:45:37

标签: java loops generics iteration hashset

  

解决。如果好奇,请参阅底部编辑。

我正在为我的计算机科学课程编写一个编程任务,我们正在为使用Quad树的基于2D图形的游戏创建一个实现。我在使用QuadTree实现的特定部分时遇到了问题。

这是属性和构造函数:

public class QuadTreeNode<E extends BoxIntersectable> {

protected Box region;

protected HashSet<E> items;

protected QuadTreeNode<E> northEast; // Quadrant 2, left 2
protected QuadTreeNode<E> northWest; // Quadrant 1, left 1
protected QuadTreeNode<E> southEast; // Quadrant 4, right 2
protected QuadTreeNode<E> southWest; // Quadrant 3, right 1

protected int depth;

public static final int MAX_items = 20; // # of items per node
public static final int MAX_DEPTH = 20;

public QuadTreeNode(Box bbox, int depth) {
    items = new HashSet<E>();
    northEast = northWest = southEast = southWest = null;
    this.region = bbox;
    this.depth = depth;
}

这是findIntersecting方法,它是递归辅助方法:

    /**
 * Find all items intersecting a given bounding box.
 * 
 * <p>
 * Hints: Use a recursive algorithm with a helper. Create a HashSet to store
 * the items and pass it to the recursive calls.
 * </p>
 * 
 * <p>
 * Recurse on any child node whose region intersects the box. At a leaf node
 * test each item stored in the node to see if it intersects the box. If it
 * does, add it to the HashSet that is returned.
 * </p>
 * 
 * @param box
 *            The box to intersect objects with.
 * @return A set containing all of the items.
 */
public Set<E> findIntersecting(Box box) {
    Set<E> intersectingBoxes = new HashSet<>();
    if (getBoxRegion().intersects(box)) {
        findIntersectingHelper(intersectingBoxes, this, box);
    }
    return intersectingBoxes;
}

/**
 * Helper method for findIntersecting.
 * 
 * @param intersectingBoxes the set of boxes intersecting box.
 * @param root the node 
 * @param box the box to be checked for intersection.
 */
private void findIntersectingHelper(Set<E> intersectingBoxes, QuadTreeNode root, Box box) {
    // No need to check if root box region intersects box, method is only called if such occurs.
    if (root.isLeaf()) {
        for (E item : root.allItems()) {
            intersectingBoxes.add(item);
        }
    } else {
        // Call recursion on any child who intersects box.
        if (northWest.getBoxRegion().intersects(box)) {
            findIntersectingHelper(intersectingBoxes, northWest, box);
        }
        if (northEast.getBoxRegion().intersects(box)) {
            findIntersectingHelper(intersectingBoxes, northEast, box);
        }
        if (southWest.getBoxRegion().intersects(box)) {
            findIntersectingHelper(intersectingBoxes, southWest, box);
        }
        if (southEast.getBoxRegion().intersects(box)) {
            findIntersectingHelper(intersectingBoxes, southEast, box);
        }
    }
}

我的问题在于findIntersectingHelper,特别是行:

for (E item : root.allItems()) {

方法allItems将属性项作为Set返回。我认为这很好,因为这是迭代一组Es,但编译器要求不同。我收到以下编译时错误:

  

类型不匹配:无法从元素类型Object转换为E

如何解决这个问题,以便编译器批准我正在尝试做什么?

感谢您的协助!

编辑:我的问题是我的递归方法调用没有泛型类型root。我是个笨蛋。我向任何我做过facepalm的人道歉。

0 个答案:

没有答案