• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Fail to create generic array?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, all:

Below is the code for a generic TreeNode class:


If you look at the two lines marked with *** and %%%, you'll see my problem:
Why does it work with line %%% but whenever I try to change *** into "children = new TreeNode<E> [numberOfChildren];", I get error saying "cannot create generic array of TreeNode<E>"?


I'm so confused why the proposed change is wrong.

Thanks in advance.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's complicated. As I explained in your other topic, there are some limitations with generics in Java, mainly because Sun wanted to keep everything fully backwards compatible when generics were added in Java 5.

One of the things they did was that they implemented generics with type erasure. This has some side effects, one of which is that you cannot create arrays of parameterized types.
 
Castor Tack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Well, it's complicated. As I explained in your other topic, there are some limitations with generics in Java, mainly because Sun wanted to keep everything fully backwards compatible when generics were added in Java 5.

One of the things they did was that they implemented generics with type erasure. This has some side effects, one of which is that you cannot create arrays of parameterized types.




Thanks for the reply. I understand what you mean. But the strange thing is, it works for line %%%.
I mean, the proposed code for line *** has the same structure as the code in %%%. Why does the compiler has different reactions?

Thanks.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Castor Tack wrote:I mean, the proposed code for line *** has the same structure as the code in %%%. Why does the compiler has different reactions?


The two constructs are vastly different. In the first case you're creating an array, but in the second case you're creating just a single object and passing an int to its constructor. You know that the object you're creating represents an array, but is it an abstract, higher-level reasoning that is irrelevant to the compiler.
 
Castor Tack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Martin:

Thanks for the patience, Martin.
I got it.

The confusion was I thought when I create a node, an array is also created since it is a data member of the node class. It was a very typical C-transition-to-Java mistake. children is just a pointer to an array, whose value is the address, not the real array.

I'm clear now.
Cheers
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic