bounded-wildcard Questions
4
Suppose that I have the following class:
public class Either<A, B> {
public Object get();
}
Either is a type that stores one object of either type A or B. get() retrieves that one object....
Backslide asked 1/9, 2012 at 17:49
2
Solved
I have this class
public class Tree<T> {
//List of branches for this tree
private List<Tree<? super T>> branch = new ArrayList<Tree<? super T>>();
public Tree(T t...
Sub asked 30/8, 2012 at 15:14
1
Solved
Class<? extends Integer> will compile fine, but Integer is a final type so it doesn't make sense to use it as an upper bound (nothing will ever extend it).
If you try to use a final type as ...
Bateman asked 11/8, 2012 at 21:17
6
Solved
Who could me explain this?
I have these couple of classes:
abstract class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
public void woof...
Baa asked 24/5, 2012 at 19:8
4
Solved
I am about to create a factory which creates objects of a certain type T which extends a certain class A and another interface I. However, T must not be known. Here are the minimum declarations:
p...
Personality asked 22/3, 2012 at 14:57
5
Solved
I have a Java question about generics. I declared a generic list:
List<? extends MyType> listOfMyType;
Then in some method I try instantiate and add items to that list:
listOfMyType = ne...
Brod asked 27/5, 2009 at 21:10
1
Solved
Are there any advantages of using wildcard-type generics in the Bar class over completely skipping them?
public class Foo<T> {}
public interface Bar {
public void addFoo(Foo<?> foo);...
Hog asked 6/2, 2012 at 18:4
2
Solved
Please explain this generic code wildcard compile time error:
//no compile time error.
List<? extends Number> x = new ArrayList<>();
//compile time error.
List<? extends Number&g...
Onehorse asked 5/2, 2012 at 5:25
3
Solved
I've read in various places including here that having a bounded wildcard in a method return type is a bad idea. However, I can't find a way to avoid it with my class. Am I missing something?
The ...
Remarkable asked 23/1, 2012 at 2:1
3
Solved
So I have this method:
protected void collectSelectedItems(ListSelectionModel lsm,
Collection<? super MyItemClass> result) {
for (int i : GUI.getSelectionIndices(lsm))
{
result.add(getI...
Indigoid asked 6/12, 2011 at 16:39
1
I have a quick question as below:
Here's a simple examples about this whole issues:
List a = new ArrayList();
List <?> b;
List <? extends Object> c;
According to Java SCJP by khalid ...
Omora asked 16/8, 2010 at 0:54
3
Solved
Please help me with this:
If Lion IS-A Animal and given Cage<T>:
Cage<? extends Animal> c = new Cage<Lion>(); // ok,
but
Set<Cage<? extends Animal>> cc = new Hash...
Kilo asked 20/5, 2010 at 18:11
7
This is NOT homework.
Part 1
Is it possible to write a generic method, something like this:
<T extends Number> T plusOne(T num) {
return num + 1; // DOESN'T COMPILE! How to fix???
}
...
Geneva asked 14/5, 2010 at 13:37
2
Solved
This following is from generics tutorials:
Say class R extends S,
public void addR(List<? extends S> s) {
s.add(0, new R()); // Compile-time error!
}
You should be able to figure out why...
Shantel asked 4/12, 2009 at 6:49
2
Is there a difference between
<N extends Number> Collection<N> getThatCollection(Class<N> type)
and
Collection<? extends Number> getThatCollection(Class<? extends Num...
Columbus asked 17/11, 2009 at 17:0
3
Can someone explain to me why the following code does not work?
public class Test {
interface Strategy<T> {
void execute(T t);
}
public static class DefaultStrategy<T> implements...
Oram asked 5/11, 2009 at 22:33
1
Solved
Java can often infer generics based on the arguments (and even on the return type, in contrast to e.g. C#).
Case in point: I've got a generic class Pair<T1, T2> which just stores a pair of v...
Yellowtail asked 18/8, 2009 at 14:24
2
I have a question about Generics in Java, namely using wildcards. I have an example class GenClass like this:
public class GenClass<E> {
private E var;
public void setVar(E x) {
var = x...
Decoder asked 2/2, 2009 at 16:1
© 2022 - 2024 — McMap. All rights reserved.