Results 1 to 12 of 12
  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Bounded Type Parameters

    Java Code:
    interface Comparable<T> {
    
    	int compareTo(T o);
    }
    
    class BoundedTypeMethods {
    
    	public static <T extends Compareble<T>> int countElements(T arr[], T element) {
    		int count = 0;
    		for (T e : arr) {
    			if (e.compareTo(element) > 0)
    				count++;
    		}
    		return count;
    	}
    
    	public static void main(String[] args) {
    
    		int arr[] = { 1, 3, 4, 5, 6, 7, 4, 3, 5, 6, 90 };
    		int element = 5;
    		BoundedTypeMethods.countElements(arr, element); // Bound Mismatch
    
    	}
    
    }
    Whats wrong with this code showing Bound Mismatch error at line 21
    Last edited by allaudin; 08-04-2013 at 06:16 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Bounded Type Parameters

    Primitives don't take a part in the entire generics hoopla; try Integer instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Bounded Type Parameters

    doesn't work with Integer as well

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Bounded Type Parameters

    The name of the interface is Comparable, not Compareble. Your IDE or compiler would've told you that, I'm sure, if only you would've read ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Bounded Type Parameters

    still saying "Bound mismatch: The generic method countElements(T[], T) of type BoundedTypeMethods is not applicable for the arguments (Integer[], Integer). The inferred type Integer is not a valid substitute for the bounded parameter <T extends Comparable<T>>"

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,686
    Rep Power
    5

    Default Re: Bounded Type Parameters

    Uh, are you defining your own Comparable interface, if I have to believe your original post? Because Integer does not automatically implement YOUR Comparable interface, it implements the one built into the JDK.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Bounded Type Parameters

    Darn, I missed that interface definition completely; indeed Integer doesn't implement that Compareble interface; problem solved.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Bounded Type Parameters

    Actually it was written in Oracle Java tutorials ..... problem solved .. thanks josAh and gimbai2

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,686
    Rep Power
    5

    Default Re: Bounded Type Parameters

    Ah but tutorials are not to be followed blindly, they are to be understood and interpreted.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Bounded Type Parameters

    implementation is also necessary ..

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Bounded Type Parameters

    An Oracle tutorial writing 'Compareble'? I don't believe that ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

Similar Threads

  1. Bounded WildCard as Type Parameter
    By jim829 in forum Advanced Java
    Replies: 0
    Last Post: 07-29-2013, 01:53 AM
  2. Replies: 2
    Last Post: 01-24-2013, 10:57 AM
  3. type of parameters in multithreading system
    By ibtehal_n in forum Threads and Synchronization
    Replies: 11
    Last Post: 09-26-2011, 06:18 PM
  4. Bounded Buffer
    By babs2010 in forum New To Java
    Replies: 0
    Last Post: 04-08-2010, 05:24 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
  •