I'm sure this has been asked before but I am new to Java and unfamiliar with the exact terminology I'm looking for.
I have a BST class:
public class BinarySearchTree<T extends Comparable> {
/* ... */
}
Then I wrote some tests for it:
public static void main(String[] args) {
Integer[] integerItems = {1, 7, 8, 2, -1, -10, 100, 12, 32};
String[] stringItems = {"jungi", "phil", "bob", "leslie", "tyler", "clarence"};
Comparable[][] comparableLists = {integerItems, stringItems};
for (Comparable[] list : comparableLists) {
BinarySearchTree<>...
}
}
I am confused at this step. How can I recover the types (String[], Integer[], etc.) from the list and use them as an argument? I want to have something like this:
for (Comparable[] list : comparableLists) {
BinarySearchTree<typeOf(list)> root = new BinarySearchTree<typeOf(list)>();
/* ... tests ... */
}
One option I found here was just to list out all the possible supported types. This seems really silly because I don't know all the supported types. Maybe these types will change, etc. but I had it hard-coded.
How can I deal with this best?
EDIT:
So just to be a little more specific, here is the BST implementation:
public class BinarySearchTree<T extends Comparable> {
private T value;
private BinarySearchTree<T> leftChild;
private BinarySearchTree<T> rightChild;
public BinarySearchTree() {
}
public BinarySearchTree(T v) {
value = v;
createChildren();
}
public void createChildren() {
leftChild = new BinarySearchTree<T>();
rightChild = new BinarySearchTree<T>();
}
public void insert(T v) {
if (value == null) {
value = v;
createChildren();
} else if (v < value) {
leftChild.insert(v);
}
rightChild.insert(v);
}
public boolean valueExists(T v) {
if (value == null) {
return false;
} else if (value == v) {
return true;
} else if (v < value) {
return leftChild.valueExists(v);
}
return rightChild.valueExists(v);
}
public String toString() {
String bstStringBuilder = "";
if (value == null) {
return "";
}
bstStringBuilder += leftChild + " ";
bstStringBuilder += value + " ";
bstStringBuilder += rightChild;
return bstStringBuilder;
}
}
If I use @OldCurmudgeon's suggestion the main() looks like this:
public static void main(String[] args) {
Integer[] integerItems = {1, 7, 8, 2, -1, -10, 100, 12, 32};
String[] stringItems = {"jungi", "phil", "bob", "leslie", "tyler", "clarence"};
Comparable[][] comparableLists = {integerItems, stringItems};
for (Comparable[] list : comparableLists) {
BinarySearchTree<Comparable> root = new BinarySearchTree<Comparable>();
for (Comparable item : list) {
root.insert(item);
}
System.out.println(root);
}
}
This produces the following compiler error:
BinarySearchTree.java:26: error: bad operand types for binary operator '<'
} else if (v < value) {
^
first type: T
second type: T
where T is a type-variable:
T extends Comparable declared in class BinarySearchTree
BinarySearchTree.java:37: error: bad operand types for binary operator '<'
} else if (v < value) {
^
first type: T
second type: T
where T is a type-variable:
T extends Comparable declared in class BinarySearchTree
2 errors
Perhaps this is more helpful?