Java hold Object "2" ArrayList container Continuation Solution

Source: Internet
Author: User

This is the explanation of the JDK API1.6.0 for ArrayList.

ArrayList

Creating classes with Java generics is complex, but applying predefined generics is simple. For example, to define the ArrayList used to hold an Apple object, you can declare Arraylist<apple>, which is a type parameter within the angle brackets (which can be multiple). It specifies the type that the container can hold.

By using generics, you can prevent objects of the wrong type from being placed in the container at compile time .

The ArrayList is transformed upward into a list.

It should be noted that the type conversion is not necessary when the element is removed from the list. Because list performs a transformation for you when it calls get (), he knows what type you are saving.

This way, by using generics, not only does the compiler check the type of objects you place in the container, but you can use a clearer syntax when using objects in the container. At the same time, you can use foreach to select each element of the list.

Referencing the code from the previous blog, if you create multiple classes that inherit the Apple () class , such as Class Fuji extends Apple () {}, then use the predefined generic arraylist<apple>   Apples=new arraylist<apple> ();   , use the Add () method to add an object instance of each Apple subclass, such as Apples.add (New Fuji ()); , and then output using the Foreach method, the result is similar to [email protected] .

Parsing: The output of the program is generated from the default ToString () method of object. The method prints the unsigned hexadecimal representation of the class name, followed by the hash code (the hash code is generated by the Hashcode () method).

Java holds object "2" ArrayList container continuation

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.