Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

java - Generic Array Creation Compilation Error From Inner Class

I'm trying to implement a linked collection using generics, something like the following.

public class A<E> {

  private class B {

    private B[] b;
    private E item;

    private B() {
      this.b = new B[2];
    }

  } // end inner class B

} // end class A

A is the collection and B an element or node in the collection with an array referencing successors/predecessors and an item.

The array creation is not allowed. The error I get is generic array creation. Am I right to think that what it's actually creating is an array of A<E>.B?

If not, what's causing the error?

If so, how can I get around this?

I have obviously omitted a substantial amount of code, if what I've provided is not enough please let me know. Any advice would be appreciated. Thank you.

EDIT 1: I should have mentioned that the parameterized type must be the same in A as in B. So passing <E> to the inner class is not possible, as it creates E#2 and leaves A with E#1.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You call B inherits the generic from the outer class, as it is not static. And you can't just make it static, because it will then need E also.

So your B.b array will indeed need a type that is generic, i.e. A<E>.B or if you'd change your code to a static inner class, A.B<E> (if you would use private static class B<E>).

In Java, due to the way generics are implemented (by erasure), the type of the array is not well-defined. On one hand, it should be an array of B, on the other hand, it should be an array of Object.

The most workable solution seems to be to use Object[] and cast explicitly. If you want increased type safety, you can of course use an ArrayList<B>, which internally uses Object[], too!

In you particular code, B b1, b2; might also be an option which is actually faster (no bounds checking) and needs less memory (no array object; no size information).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...