ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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);
}
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>
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.