Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-17-2009, 11:32 PM
Member
 
Join Date: Nov 2009
Posts: 3
Rep Power: 0
stijn1989 is on a distinguished road
Post Creating generic arrays
Hello

I followed a conference about java generics at Devoxx. The person who gave the presentation was a prof. at the university of Leuven, Belgium. He told that you can't create an array of generics. Let me give an example.

Code:
T [] names = new T[10];
The new T[10] part gives the error. That's something I don't understand. The T in this piece of code is replaced by the name of the class during the compiling. So this should change to this for example:

Code:
String [] names = new String[10];
So during runtime this creates a array holding Strings. I understand that you can't do the follow thing because you don't know the constructor of T.

Code:
T obj = new T;
Can someone explain why I can't create a generic array. Thanks!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-17-2009, 11:37 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
I'm assuming because there is no generics constructor which when you create an array you need. Does T[] names = null work?
__________________
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-17-2009, 11:47 PM
Member
 
Join Date: Nov 2009
Posts: 3
Rep Power: 0
stijn1989 is on a distinguished road
Default
Originally Posted by xcallmejudasx View Post
I'm assuming because there is no generics constructor which when you create an array you need. Does T[] names = null work?
T[] names = null; works. But if you create an array you create an array with null references and no references to T. That's why I don't understand it. You can't initialize the array, so the follow should not work.

Code:
T [] names = new T{new T(...) , ....};
Does the compiler needs to know the size of the array during compile time? I don't think so because during runtime the array can grow and shrink...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-17-2009, 11:49 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
Wrong. An array cannot change sizes. An ArrayList however can. Try creating a generic ArrayList that might give you better results.
__________________
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-18-2009, 10:44 AM
Senior Member
 
Join Date: Apr 2009
Posts: 791
Rep Power: 1
Tolls is on a distinguished road
Default
It's all, apparently, to do with type erasure.
There's a brief explanation here in the reply by gafter (first reply).
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-18-2009, 01:21 PM
Member
 
Join Date: Nov 2009
Posts: 3
Rep Power: 0
stijn1989 is on a distinguished road
Default
So it's because array's are dynamic and generic's are static? What does that mean "array's are dynamic"? Is it because you can change an array to another array?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-18-2009, 02:26 PM
Senior Member
 
Join Date: Apr 2009
Posts: 791
Rep Power: 1
Tolls is on a distinguished road
Default
His example was pretty clear I thought.
By allowing generic arrays it opens up some potential errors which generics were there to solve when handling Lists. In other words that casting a generic array to an Object array, assigning an object of a different generic type to it, then accessing that object through the original generic array...and bang.

Since, as I say, this is the sort of thing generics were brought in to solve (remember being able to put any old rubbish into a List?)...
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Creating a constructor with arrays and arguments - Part 2 fullmetaljacket New To Java 15 07-05-2009 02:38 PM
[SOLVED] Creating a constructor with arrays and arguments fullmetaljacket New To Java 38 07-03-2009 07:09 AM
Generic methods andre1011 Advanced Java 7 02-25-2009 03:17 PM
[SOLVED] Creating an Array of Arrays? xcallmejudasx Advanced Java 5 11-04-2008 07:01 PM
Generic array eva New To Java 3 12-23-2007 01:12 AM


All times are GMT +2. The time now is 12:56 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org