• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics...

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preetha,

What do you want to achieve here?
Generic array creation is not allowed.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Deepak,

i tried with constructor it works.

Preetha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic