Generics and Arrays

N

nickclare

Hi everyone,

I'm trying to write a very simple B-tree implementation, just as a
learning exercise. I thought it would be interesting to try to use
Generics, becuase I haven't used the new features in 1.5 much, and
wanted to learn.

Now, I do understand that you can't create arrays from type variables
directly, but rather have to use something like the following:

public class BTree<D extends Comparable> {

//TEMPORARY, TILL I CAN GET THIS WORKING
public static final int NODE_SIZE = 10;

Class<D> dClass;
Class<D[]> dArrayClass;

public BTree(Class<D> d, Class<D[]> dArray) {
dClass = d;
dArrayClass = dArray;
...
}

...

private class Node<D extends Comparable> {
Node<D> parent;
Node<D>[] children;
D[] data;

public Node() throws Exception {
Object temp = java.lang.reflect.Array.newInstance(dClass,
NODE_SIZE);
data = dArrayClass.cast(temp);
...
}
}

Now, this creates the data array, but how can I create the children
array?
There is no way to create the class object representing Node<D>[], even
though I have the class object representing D. The only way I can see
is having it passed to the BTree constructor, but this would require
Node being public, and apart from that, the BTree constructor call is
too long already.

Any help would be greatly appreciated.
Thanks,
Nicholas Clare
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Generics ? 14
generics puzzle 57
generics and arrays and multi-class collections 22
Tree design with generics 2
Generics 24
Generics 12
Generics Amanuensis? 2
Generics and Polymorphism 5

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top