Learning--java paradigm from cannot create a generic array of arraylist<xx>

Source: Internet
Author: User

Recently, when writing a program in Java, I want to write an array that includes arraylist<xx>.

Define yourself as follows:

arraylist<edge>[] edges = new arraylist<edge> () [10];

Then the compiler error is as follows:

cannot create a generic array of arraylist<edge>;


In C + + can be such a more convenient definition: vector<edge> a[100];

In the Java error, query a bit, see already someone in StackOverflow asked this question, the link is as follows:

Http://stackoverflow.com/questions/4549192/create-an-array-of-arrayliststring-elements

The above link is mostly to solve (replace) The method, the specific reason for the design to continue to delve into their own.


First, the cause of this problem

As the IDE's error message says: You cannot create an array of patterns, the underlying array type can only be object.

Type erase:

Simply put, the JVM treats the generic storage as object.

That is Arraylist<integer>.class = Arraylist<string>.class = Arraylist<object>.class

So if you define a generic array, the "Cat Insert Dog List" event is likely to occur when you use it.

Then we go on to explore why the type erasure was introduced, <<thinking in java>>, because there was no generics before JavaSE1.5,

so for the previous version class libraries are fused (without rewriting the previous class library), so the type erase method is currently used.

Ii. Methods of Settlement

1, very simple, no generics. An additional class T is defined, and T contains a ArrayList member variable, which is added to the ArrayList by the public method add of T.

2, method One is the solution to my program, for Java generics more general method, class XX <T> {},

<<thinking in Java>> says four solutions, as follows:

http://blog.csdn.net/eric_sunah/article/details/7262486

There is no way to overturn the underlying array type, it can only be object[]

So the first three ways the authors talk about the transformation between object and other types can throw exceptions or fail to compile

Therefore, the fourth method described in the link above should be used to pass the type tag <T> to the constructor in order to recover from the erase

Learning--java paradigm from cannot create a generic array of arraylist<xx>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.