(reprint) New arrays in Java and ArrayList Java does not allow generic arrays

Source: Internet
Author: User

New Array in Java:

String[] s;//is defined without the need to set the size S = new string[5];//to set the size of the array when allocating space for arraylist,arraylist<string> result = new arraylist& Lt String> ();//This time you do not need to set the size, Result.add (string) to add an element, Result.remove (string) Delete an element, the size can be obtained by result.size () Again see Java allow map[] Maparray = new map[5]; also allow map<integer,string>[] Maparray = new map[5]; Map<integer,string not allowed >[] Maparray = new map<integer,string>[5];//does not allow new to be an array with a generic typein the case of Java SE 1.5, without generics, by referencing the type object to implement the "arbitrariness" of the arguments, the disadvantage of "arbitrariness" is to make explicit coercion of type conversions, and this conversion is required by the developer for the actual parametersThe type can be predicted in the case of the. The benefit of generics is that it checks for type safety at compile time, and all casts are automatic and implicit, increasing the rate of reuse of the code. Generics are the types that are displayed to tell the compiler container, such as map<integer,string> is a generic type.

(reprint) New arrays in Java and ArrayList Java does not allow generic arrays

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.