Generic array list and reflection

Source: Internet
Author: User

If you declare a generic array that does not have a space length specified, such as arraylist<integer> in = new
Arraylist<> (), the system will automatically allocate 10 capacity space for in, that is, [null,null,null,null,null,null,null,null,null,null], if this is the in assignment, such as:

 for (int i=1;i<=5;i++) {    
In.add (i);
}

In this case, the content of in is actually [1,2,3,4,5,null,null,null,null,null], which is five null values, but 5 is obtained with the In.size () method, excluding the subsequent null, when implementing the ToString () method of reflection, The following null values may be printed, such as:

Java.util.arraylist[elementdata=class java.lang.object[]{java.lang.integer[value=1][][], Java.lang.integer[value=4][][],
Java.lang.integer[value=9][][],java.lang.integer[value=16][][],java.lang.integer[value=25][][],null, null,null,null,null},
Size=5][modcount=5][][]
Even if the size of the ArrayList is specified initially, such as arraylist<integer> in = new arraylist<> (4), the size of the ArrayList itself will grow dynamically when the array element is dynamically increased to a certain amount. The growth value is 4->7->11->17 ..., instead of increasing the capacity one by one, follow the rules ((Old capacity * 3)/2) + 1.
In addition, an array is an object in Java, which means it can be referenced as such:
int New int [publicstatic  Object XXX (Object a) {       ...}

Generic array list and reflection

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.