aspose file tools*
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
Author

Generic List array

Ganesha Kumar
Ranch Hand

Joined: May 04, 2006
Posts: 56
Hi
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?

Thanks,
Ganesh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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 7 API documentation
Scala Notes - My blog about Scala
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[].


or

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
Bartender

Joined: Mar 24, 2008
Posts: 3829

Jesper Young
------------
See my blog post for details.


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

SCJP 5 | SCWCD 5
[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
// http://java.sun.com/javase/6/docs/api/java/util/List.html
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic List array