I try to create a generic array but I'm taking the error of the title.
ByteConverter<Product> byteconverter = new ByteConverter<Product>();
//into an inner class I have to declare a final field
final ByteConverter<Product>[] byteconverter2 = {byteconverter};
So, I searched at the Stackoverflow for a possible solution. I found something similar here: Cannot create an array of LinkedLists in Java...? , so I canged my code to the following:
final ByteConverter<Product>[] byteconverter2 = {(ByteConverter<Product>[])byteconverter};
but I still take the same error. I can't understand why..Any help please?
asked Dec 12, 2012 at 15:06
-
2What error are you getting?Rohit Jain– Rohit Jain2012年12月12日 15:07:22 +00:00Commented Dec 12, 2012 at 15:07
-
1Read through stackoverflow.com/questions/529085/…. You should find your answer.Georgian– Georgian2012年12月12日 15:11:45 +00:00Commented Dec 12, 2012 at 15:11
-
@Rohit Jain: Cannot create a generic array of ByteConverter<Product>Nick Robertson– Nick Robertson2012年12月12日 15:11:46 +00:00Commented Dec 12, 2012 at 15:11
2 Answers 2
final ByteConverter<Product>[] byteconverter2 =
new ByteConverter[]
{
byteconverter
};
this works well
answered Dec 12, 2012 at 15:07
-
You don't need to give type on RHS, while initializing array at the time of declaration.Rohit Jain– Rohit Jain2012年12月12日 15:08:54 +00:00Commented Dec 12, 2012 at 15:08
-
and it is applicable for java 7 onlyNandkumar Tekale– Nandkumar Tekale2012年12月12日 15:09:56 +00:00Commented Dec 12, 2012 at 15:09
This compiles, though with a warning
ByteConverter<Product> byteconverter = new ByteConverter<Product>();
ByteConverter<Product>[] byteconverter2 = new ByteConverter[] { byteconverter };
Read here http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html about restrictions for generics
answered Dec 12, 2012 at 15:18
lang-java