File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic List array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic List array" Watch "Generic List array" New topic

Generic List array

Ganesha Kumar
Ranch Hand

Joined: May 04, 2006
Posts: 56
I want a String list array, which I am trying to create using the statement:
List<String>[] listArray = new List<String>[3];
But the compiler is giving an error for 'new List<String>[3]'. If I use the statement:
List<String>[] listArray = new List[3];
it works fine. But I am not sure if it serves my purpose. Can anyone please clarify?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

You cannot create an array with elements of concrete parameterized type. The reason for this is quite technical (because generics in Java are implemented with type erasure, which is incompatible with how arrays work).

See my blog post for details.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
First of all, Ganesh, I do not get how List<String>[] listArray = new List[3]; is correct, because List is an interface and how can we do a "new" on it?
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245

does not do new on List, but on List[].


would be incorrect because List is interface.

[ October 02, 2008: Message edited by: Vlado Zajac ]

[ October 02, 2008: Message edited by: Vlado Zajac ]
[ October 02, 2008: Message edited by: Vlado Zajac ]
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3902

Jesper Young
See my blog post for details.

That's a really good one.
[ October 15, 2008: Message edited by: Vijitha Kumara ]

[How to ask questions] [Twitter]
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
// Following code my help to understand the issue you are having.

interface MyInterface {};
class MyClass implements MyInterface {};

public class MyApp {

public static void main(String[] args) {
MyInterface myInterface;

// You can't instantiate an interface, so following code would failed duing compile
//myInterface = new MyInterface();

// But you can do this, as MyClass IS-A MyInterface
myInterface = new MyClass();

// For more information about List, take a look at
I agree. Here's the link:
subject: Generic List array
It's not a secret anymore!