Results 1 to 7 of 7
  1. #1
    stijn1989 is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    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.

    Java 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:

    Java 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.

    Java Code:
    T obj = new T;
    Can someone explain why I can't create a generic array. Thanks!

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    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.

  3. #3
    stijn1989 is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote 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.

    Java 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...

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    It's all, apparently, to do with type erasure.
    There's a brief explanation here in the reply by gafter (first reply).

  6. #6
    stijn1989 is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    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?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    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?)...

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •