Results 1 to 9 of 9
  1. #1
    jdgallag is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default Generics Question

    I want to write a method that computes the sum of values in a list of anytype that is a sub type of number. I tried

    public static <T extends Number> Number summup(List<T> list){
    if (list.isEmpty()){
    return 0;
    }
    else {
    return (list.get(0) + summup(list.remove(0)));
    }
    }

    and Got an error on the use of summup(list.remove(0) in the else block, because summup
    doesn't accept values of type T. I need to get into the feel of generics, so any feedback you can give would be greatly helpful. Eventually I would like to extend this method to sum any collection, but that is a post for another day.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    Look at what you're passing into your recursive call.

    By the way, is there a reason why this method is recursive?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    When you get errors, please post the full text of the error message.

  4. #4
    jdgallag is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default

    @Norm
    "The method summup(List<T>) in the type GenericHelloWorld is not applicable for the arguments (T)"

    @emceenugget
    Oh I thought that list.remove(0) would return the list with the first element removed. Instead it returns the first element. Guess I should have read the documentation. Thanks for that. The reason I want this method recursive is so that it ignores the underlying characteristics of the collection, and will (hopefully) allow me to generalize to other collections. I actually have to have this method work for arbitrary collections eventually, so maybe I should look at doing it with an iterator too. Can you delete items from a collection through an iterator?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    I tried to get your code to work but am having trouble with the + operator. The only way I got it to compile was by explicitly casting to Integer. Without the casts I get:
    operator + cannot be applied to T,T
    Java Code:
        public static <T extends Number> Number summup(List<T> list){
    //      System.out.println(list + " " + list.getClass()); //[2, 3] class java.util.ArrayList
    
       	if (list.isEmpty()){
       	  return 0;
       	}
       	else {
            Number sum = sum2Numbers(list.remove(0) , summup(list));     //  WORKS 
            return sum;
       	}
        } // end summup()
        // Try to add two objects extended from Number
        public static <T extends Number> Number sum2Numbers(T n1, T n2) {
          return (Integer)n1 + (Integer)n2;  // works with casting >>>>>>>????????????????
        }

  6. #6
    jdgallag is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default

    Yes, that code was naive. I have no problem treating all sums as double so i rewrote the code as. It came down to the question of how generic I need to be, and as long as I can get stable answers, that is all that I require. Thank you for your insight though. Now I have to extend this (or at least attempt) for arbitrary collections.

    public static <T extends Number> double summup(List<T> list){
    if (list.isEmpty()){
    return 0.0;
    }
    else {
    return (list.get(0).doubleValue() + summup(list.subList(1, list.size())));
    }
    }

    Which will work because all Numbers have a doubleValue() method.

  7. #7
    jdgallag is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default

    This is a first attempt to get a method header down. I want to have a collection that works over values of type T. But this is not right. All I did was replace List<T> in the original code with <? extends Collections> but that is not correct. What should I do to capture the intended effect.

    public static <T extends Number> double sumgen(<? extends Collections><T> coll){

  8. #8
    jdgallag is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default

    here is working code:

    public static <T extends Number> double sumgen(Collection<T> coll){
    if (coll.isEmpty()){
    return 0.0;
    }

    else{
    Iterator<T> iterate = coll.iterator();
    double sum = 0.0;
    while (iterate.hasNext()){
    sum += iterate.next().doubleValue();
    }
    return sum;
    }

    }

    Thanks for your help all.

  9. #9
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    I think I'm gonna commit suicide, java is too much for my feeble mind to wrap around its complexities.Goodbye cruel world.
    If
    {
    (any1 reads this and think I'm serious pray for me)
    }
    else laugh along with me and my sick humor;

Similar Threads

  1. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 07:58 PM
  2. Question about java generics
    By Arrowx7 in forum New To Java
    Replies: 1
    Last Post: 08-14-2008, 02:37 AM
  3. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:08 AM
  4. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-10-2008, 11:08 PM
  5. Generics
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 09:10 PM

Posting Permissions

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