They’re called generics, not templates. Expecting C++ template-like behavior will lead to heartbreak. Anyway, you’re 99% of the way there – you just need to replace ‘E’ with ‘?’ so you end up with:
public void query([...], List<? extends QuadtreeElement> result) { [...] }
just a quick question about what you do with this “result” collection.
Do you fill it with elements in your querry method?
If I think it is not the good of a idea, beside then you should use an upper and not a lower bound. ? super CLASS instead of ? extends CLASS
Good catch. If query can only return QuadtreeElement, you can’t make it fill a List any more than a method returning Object can populate a String variable. Obviously if your query is actually getting Element instances, you can downcast them, but it’s not a terribly good idea.
public void query([...], List<? extends QuadtreeElement> results) { [...] }
Which didn’nt work. It gave me this error on a later “results.add([QuadtreeElement])” (The problem is, that I don’t know which QuadtreeElement implementing class is actually is:
[quote]The method add(capture#1-of ? extends QuadtreeElement) in the type List<capture#1-of ? extends QuadtreeElement> is not applicable for the arguments (QuadtreeElement)
[/quote]
The problem on the later call of “query” didn’t throw any errors.
So I changed it to:
public void query([...], List<? super QuadtreeElement> results) { [...] }
So now the Error on the “results.add([QuadtreeElement])” is gone, but the error on the “tree.query([…], [ArrayList])” is back:
[quote]The method query(StaticRect, List<? super QuadtreeElement>) in the type Quadtree is not applicable for the arguments (StaticRect, ArrayList<Quadtree.Element/Mind that there is the dot/>)
[/quote]
Also, since this might be helpful, here is the almost-complete source-code:
[Offtopic: Riven, the pastbin seems to be broken:
“8: Undefined variable: seek
File: /home/jgo/public_html/addon_pastebin.php
Line: 12”
So I just post it as normal pastebin:] http://pastebin.com/C0RsHqip
I need help…
The problem with using “Quadtree” is, that this would not be working:
elements = new E[elemPerQuad]; // See in the constructor
I hope you can help me
EDIT:
Yes I do fill it with “results.add([QuadtreeElement])”…
Ooookey. I fixed it. JGO’s pastebin is still not working for me…
The source code will be posted on “Shared Code”
(Editing link into it, for everyone googleing this.)
EDIT: The Topic
elements = new E[elemPerQuad]; // See in the constructor
Can’t be done. Anything that needs the type of E at runtime is impossible. In this one case, it’s easily fixed, just change elements to List. Other cases where you need something like ‘new E’ you’re out of luck
So I did a workaround.
I casted a newly-created Array to the specified chosen “E”-Array. I can be sure, that E extends QuadtreeElement, so I choose it. I could have also used Object, that was just for readablility.
You could try it out yourself: use QuadtreeElement[] yourself. You will get an error, because you can’t assign a QuadtreeElement[] to E[]. So you need to cast that array to E[].
Yes but objects are still different from Arrays. In the end I’m casting ints to ints or longs to longs and only change the “mark”, which type of class is used for this Array.
What I can tell you is: it does not work without this workaround. This workaround is solid, and works for every case
Strange when I try to run the following code in eclipse it crash :
public class TypeArray {
public static void main(String [] args){
String[] array = (String[]) (new Object[10]);
}
}
with that exception
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
at test.TypeArray.main(TypeArray.java:6)
Actually I don’t have any problems now. They are all solved.
That code I wrote gives me a warning, but it actually can’t crash, so why bother?
Also, thank you for trying to help me, but currently you only said, that that code part didn’t make sense, and why. You didn’t even give me another option, or an Idea of how to do it another way.