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
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