Java:遍历泛型哈希集时出现类型不匹配错误<;E>;
解决了。如有疑问,请参阅底部编辑 我正在为我的计算机科学课程做一个编程作业,我们正在为一个使用四叉树的2D图形游戏创建一个实现。我在四叉树实现的特定部分遇到了问题 这是属性和构造函数:Java:遍历泛型哈希集时出现类型不匹配错误<;E>;,java,loops,generics,iteration,hashset,Java,Loops,Generics,Iteration,Hashset,解决了。如有疑问,请参阅底部编辑 我正在为我的计算机科学课程做一个编程作业,我们正在为一个使用四叉树的2D图形游戏创建一个实现。我在四叉树实现的特定部分遇到了问题 这是属性和构造函数: public class QuadTreeNode<E extends BoxIntersectable> { protected Box region; protected HashSet<E> items; protected QuadTreeNode<E> nort
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;
}
allItems方法将属性项作为一个集合返回。我认为这很好,因为这是通过一组Es进行迭代,但是编译器要求不同。我得到以下编译时间错误:
类型不匹配:无法从元素类型对象转换为E
如何修复此问题,以便编译器批准我尝试执行的操作
谢谢你的帮助
Edit:我的问题是递归方法调用没有泛型类型root。我是个笨蛋。我向我制作facepalm的所有人致歉。您在findIntersectingHelper中声明root的类型是什么?我认为如果您对for循环执行以下操作,它将起作用<代码>用于(框可相交项:root.allItems()){。由于从根目录返回的所有项目都将是类型/子类型为
BoxIntersectable
。我找到了答案。我的问题是我在递归函数调用中没有使用泛型类型root。现在工作正常。您在findIntersectingHelper中声明根目录的类型是什么?我想如果您按照下面的方法执行for lo,它会工作op.for(BoxIntersectable项:root.allItems()){
。因为从root返回的所有项都将是BoxIntersectable
的类型/子类型。我解决了这个问题。我的问题是在递归函数调用中没有泛型root。现在工作正常。
/**
* 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);
}
}
}
for (E item : root.allItems()) {