Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

generics...

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preetha,

What do you want to achieve here?
Generic array creation is not allowed.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Deepak,

i tried with constructor it works.

Preetha
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic