Results 1 to 4 of 4
Thread: Need Help with Using Generics
- 04-09-2011, 07:49 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Need Help with Using Generics
Hi Guys,
I have a problem while working with generics. In my code below, I am trying to pass in t1 and t2 binary search tree object into a method in binary search tree(BST) iteslf. purpose is to combine all items that t1 and t2 intersects and insert into t3.
from the main application (all my generics are set as Customer object):
public static void intersect (BST<Customer> t1, BST<Customer> t2, BST<Customer> t3) {
t3.intersect (t1,t2);
}
now at the BST(all generics uses <E> type), method intersect :
public void intersect (BST<E> t1, BST<E> t2){
if (t1.getRoot() == null || t2.getRoot() == null) return;
intersect2 (t1.getRoot(),t2.getRoot());
}
public void intersect2 (BSTNode<Customer> t1, BSTNode<Customer> t2){
int id = t1.item.getAccountID();//item is a customer object
System.out.println (id);
}
the java compile states that t1.item in intersect2 method is of E type. it don't work even if i cast it. Can any one help me?
Warmest Regards
badbeauty
- 04-09-2011, 08:01 AM #2
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
correction
Sorry, the intersect2 should be:
public void intersect2 (BSTNode<E> t1, BSTNode<E> t2){
int id = t1.item.getAccountID();//item is a customer object
System.out.println (id);
}
- 04-09-2011, 08:05 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Is itersect2() part part of the BST class? Because it shouldn't be. The BST class is generic: that is it shouldn't know or care what the E type is other how it was declared. In particular methods that deal with customers and their account id's etc don't belong in the generic tree class.
Most likely intersect2() should be declared to take BSTNode<E> arguments.
purpose is to combine all items that t1 and t2 intersects and insert into t3
At the moment the problem arises because of the System.out.println() statement, but you don't mention printing anything as being any part of the purpose. If you are printing things in order to see what is happening just print the BSTNode<E> that you are working with. (and give that class a useful toString() method).
- 04-09-2011, 08:07 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Help with generics
By shai in forum New To JavaReplies: 0Last Post: 08-12-2010, 07:07 AM -
Generics
By sakshamkum in forum Advanced JavaReplies: 3Last Post: 06-03-2010, 10:12 PM -
generics
By tascoa in forum Forum LobbyReplies: 2Last Post: 10-09-2008, 07:58 PM -
Help w/ generics
By Hollywood in forum New To JavaReplies: 2Last Post: 02-16-2008, 03:08 AM -
Generics
By sireesha in forum New To JavaReplies: 2Last Post: 01-10-2008, 11:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks