Results 1 to 4 of 4
  1. #1
    badbeauty is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    badbeauty is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default 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);
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    OK! So the intersect2() method has the right arguments. My point stands: this is a generic class and so should not be dealing with the details of the specific type that is the payload of the bst.

Similar Threads

  1. Help with generics
    By shai in forum New To Java
    Replies: 0
    Last Post: 08-12-2010, 08:07 AM
  2. Generics
    By sakshamkum in forum Advanced Java
    Replies: 3
    Last Post: 06-03-2010, 11:12 PM
  3. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 08:58 PM
  4. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 04:08 AM
  5. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-11-2008, 12:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •