• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fail to create generic array?  RSS feed

 
Castor Tack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!