LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2011, 05:30 PM   #1
0raven0
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Rep: Reputation: 0
Java Generics


Hey all,

I'm pulling my hair apart to figure out why this isn't working. Essentially, I want to create a utility method that uses the binarysearch static method to get the index and insert into the collection. Oddly, this doesn't compile!

Code:
public static <C> void binarySearch1(List<? extends Comparable<? super C>> list, C key) 
{
   int index = Collections.binarySearch(list, key);		  
   list.add(index, key); // doesn't compile here
}
Quote:
The method add(int, capture#3-of ? extends Comparable<? super C>) in the type List<capture#3-of ? extends Comparable<? super C>> is not applicable for the arguments (int, C)
 
Old 10-03-2011, 08:42 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,771

Rep: Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070
You declared list as a list of things that could be compared to C and superclasses of C, but not necessarily a list of C, try
Code:
    static <C extends Comparable<? super C>> void binarySearch1(List<C> list, C key) 
    {
        int index = Collections.binarySearch(list, key);
        list.add(index, key);
    }
 
1 members found this post helpful.
Old 10-04-2011, 05:40 AM   #3
0raven0
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Awersome it works!
 
Old 02-14-2012, 06:26 AM   #4
0raven0
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Original Poster
Rep: Reputation: 0
I was trying a variation of this but I got stuck again!

Code:
	class A {
	}

	class SubA extends A {
	}

	class B<T extends A>  {
	}

	class SubB<T extends A>  {
	}

	
	class X<T extends B<? super A>> {
	}


        new X<SubB<SubA>>(); // compilation error
Here's the error msg:
Bound mismatch: The type SubB<SubA> is not a valid substitute for the bounded parameter <T extends B<? super A>> of the type X<T>

Thanks!
 
Old 02-14-2012, 08:34 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,771

Rep: Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070
Code:
	class A {
	}

	class SubA extends A {
	}

	class B<T extends A>  {
	}

        // I assume SubB is meant to be a subclass of B
	class SubB<T extends A> extends B<T>  {
	}

        // This is a possible fix, although it's hard to tell if it makes sense
        // since all the class names are just placeholders...
	class X<T extends B<? super A>> {
	}


        new X<SubB<SubA>>(); // compilation error
 
Old 02-14-2012, 09:57 AM   #6
0raven0
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Sorry that was what I meant! Even when SubB extends B it still doesn't compile.

EDIT:

Adding the wildcard wouldn't work as I'll need to unsafely typecast the data

Last edited by 0raven0; 02-14-2012 at 09:59 AM.
 
Old 02-14-2012, 12:38 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,771

Rep: Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070
Quote:
Originally Posted by 0raven0 View Post
Sorry that was what I meant! Even when SubB extends B it still doesn't compile.

EDIT:

Adding the wildcard wouldn't work as I'll need to unsafely typecast the data
My proposed fix was to remove "super A" from "class X<T extends B<? super A>>", I didn't add anything. Perhaps you can add some code using X to better explain your requirements.
 
  


Reply

Tags
java


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Java Woes: A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available ... chytraeus Slackware 10 11-27-2010 10:04 AM
Generics in C? MTK358 Programming 7 02-16-2010 07:07 AM
LXer: Java news met with cautious optimism in free Java community LXer Syndicated Linux News 0 11-14-2006 10:21 PM
SYSERR(root): hash map "generics": missing map file /etc/mail/genericstable.db? singying304 Linux - Networking 4 02-28-2005 06:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration