| Author |
generics...
|
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
Hi ,i am just trying to understand the concept of generics and this is my own. import java.util.*; class card { public static void main(String args[]) { card[] pair=new card<?,?>[10]; pair[0]=new card<Long,Long>(0L,0L); pair[1]=new card<String,String>("",""); pair[2]=new card<String>(""); } } here i am getting compiler errors as "type card does not take parameters for each line in the main method". should i create constructor for card? if so how can i create it for unbounded wildcard parameterized type? please clear this... Thanks Preetha
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Hi Preetha, What do you want to achieve here? Generic array creation is not allowed.
|
cmbhatt
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
import java.util.*; public class card<T,E> { public card(T obj1, E obj2){ } public card(T obj1){ } public static void main(String args[]) { card[] pair=new card[10]; pair[0]=new card<Long,Long>(0L,0L); pair[1]=new card<String,String>("",""); } } You have to make generic card class. [ December 07, 2008: Message edited by: Punit Singh ]
|
SCJP 6
|
 |
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
Hi Chandra, It is allowed to create an array whose component type is an unbounded wildcard parameterized type. just go through this www.angelikalanger.com Thanks Preetha
|
 |
Deepak Jain
Ranch Hand
Joined: Aug 05, 2006
Posts: 637
|
|
Yes you should Two Fixes: Non Generic Code: Generic Code: This shows generics are power full Card[] pair = new Card<?,?>[10]; This line says that the array pair can take objects of any type . Since we have ? ? [2 ?] this implies that the objects must have a constructor that can take 2 arguments.
|
SCJP, SCWCD, SCBCD
|
 |
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
Thanks Deepak, i tried with constructor it works. Preetha
|
 |
 |
|
|
subject: generics...
|
|
|