SCJP6:80%, OCWCD 88 %
From Brazil
Originally posted by Djonatah Stiegler:
You cannot use the wildcard ? when declaring collections.
Originally posted by Sharma Shweta:
The explaination says :
Does not compile at 2. List<T> is required. Can not convert ArrayList<String> to List<T>
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
From Brazil
Originally posted by Sharma Shweta:
Source : http://www.etattva.com/
The explaination says :
Does not compile at 2. List<T> is required. Can not convert ArrayList<String> to List<T>
Can someone explain
public <T> List<T> meth(List<?> type)
{
System.out.println(type); // 1
return new ArrayList<T>();//ok 1
//return new ArrayList(); //ok 2
//List<T> assignList=new ArrayList<T>();
//return assignList;//ok 3
//List assignList1=new ArrayList<T>();
//return List<T> list=assignList1;//ok 4
//List<T> assignList2=new ArrayList();
//return assignList2;//ok 5
}
SCJP 6
SCJP6:80%, OCWCD 88 %
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
thanks
Harvinder
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
public <T extends Number> List<T> meth(List<?> type)
{
System.out.println(type); // 1
List<Integer> list1 = new ArrayList();
return list1;
}
SCJP 6
John<br />SCJP 5, 97%
SCJP6:80%, OCWCD 88 %
SCJA 96%
SCJP 6 88%
skipping SCJD to work on passing SCWCD
John<br />SCJP 5, 97%
John Grabowsky wrote:
T is not known at compile time here (can be Number, Byte, ...), the difference is that T is bounded. If the extends Number is removed, we can return only generic types that satisfy <? super T> for any T, that is Object and T. If, in addition, the ? super is removed (now we are back to Shweta's initial example), we return only generic types equal to T for any choice of T, so T itself is our only choice.
All code in my posts, unless a source is explicitly mentioned, is my own.
John<br />SCJP 5, 97%
All code in my posts, unless a source is explicitly mentioned, is my own.
It's a tiny ad. At least, that's what she said.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|