Java Generic problems.

I suggested using ArrayList in his QuadTree thread too. It’s superior to using arrays.

Yes, this could be an option. But the problem is, that I want to use as low memory as possible, and as I have fixed-size elements per quad anyways, I don’t need lists.

(Also Lists do almost the same hack I do internally… They use an Object[] and then cast the elements to (E).)

Let put it all together…

First, you asked for that :

Then you show the code and we can see that :

 public void query(StaticRect range, List<? super QuadtreeElement> results) {

Which probably don’t do the thing you want to do since you asked for calling it for any subclass of QuadtreeElements and now it call it for any superclass of QuadtreeElements…

What you really wanted to do is probably that :


public class Quadtree<E extends QuadtreeElement>{
    public void query(StaticRect range, List<E> results){}
}

or

public <E extends QuadtreeElement> void query(StaticRect range, List<E> results){}

The memory difference is extremely negligible in this case - a few Objects vs. a few arrays.

This is a perfect example of “premature optimization” – introducing ugly hacks as a means of optimizing, when in reality it will make zero noticeable difference in memory/performance.