Java Learning (5)--ArrayList

Source: Internet
Author: User

How do I dynamically view the capacity of ArrayList?

Among the methods provided by ArrayList, there are three methods:

Size ()--Returns the number of elements in the current list;

TrimToSize ()--Adjusts the current ArrayList capacity to the current size of the list;

ensurecapacity (int mincapacity)--If you need to expand the capacity of the list to ensure that mincapacity elements can be loaded;

How to dynamically monitor the overall list of capacity changes?

Please refer to the following code:

  Import Java.lang.reflect.Field;

Importjava.util.ArrayList; Public classarraysizetest {@SuppressWarnings ("Rawtypes")     Public Static voidMain (string[] args) {//TODO auto-generated Method StubArraylist<string> Testarray =NewArraylist<string> (5);        Field Al_field;        Field al_field_1;        Field al_field_2;        Field Al_field_3; Try {            //Check the initial capacity of TestarrayClass Al_class =Testarray.getclass (); Al_field= Al_class.getdeclaredfield ("Elementdata"); Al_field.setaccessible (true); Object[] Objects=(object[]) al_field.get (Testarray);            System.out.println (objects.length);            System.out.println (Testarray.size ()); //result:capacity = 5, size = 0; //Add 4 items in the TestarraySystem.out.println ("1::>" +Testarray.hashcode ());  for(inti=0; i<4; i++){
Testarray.add (i, integer.tostring (i));
System.out.println ("1.") +i+ "::> " + testarray.hashcode ()); } System.out.println (objects.length); System.out.println (Testarray.size ()); System.out.println (
"2::>" +Testarray.hashcode ()); //result:capacity = 5, size = 4; //result:new a ArrayList//After trimtosizetestarray.trimtosize (); //Re-get the ObjectsClass Al_class_1 =Testarray.getclass (); Al_field_1= Al_class_1.getdeclaredfield ("Elementdata"); Al_field_1.setaccessible (true); Object[] Objects_1=(object[]) al_field_1.get (Testarray); System.out.println (objects_1.length); System.out.println (Testarray.size ()); //result:capacity = 4, size = 4; //larger the capacity to 6System.out.println ("3::>" +Testarray.hashcode ()); Testarray.ensurecapacity (6); Testarray.add ("5"); Testarray.add ("6"); Testarray.add ("7"); System.out.println ("4::>" +Testarray.hashcode ()); //After adding item the Testarray is re-created//Re-get the ObjectsClass al_class_2 =Testarray.getclass (); Al_field_2= Al_class_2.getdeclaredfield ("Elementdata"); Al_field_2.setaccessible (true); Object[] Objects_2=(object[]) al_field_2.get (Testarray); System.out.println (objects_2.length); System.out.println (Testarray.size ()); System.out.println ("5::>" +Testarray.hashcode ()); //result:capacity = 9, size = 7; //Do not create a new ArrayListtestarray.trimtosize (); //Re-get ObjectsClass Al_class_3 =Testarray.getclass (); Al_field_3= Al_class_3.getdeclaredfield ("Elementdata"); Al_field_3.setaccessible (true); Object[] Objects_3=(object[]) al_field_3.get (Testarray); System.out.println (objects_3.length); System.out.println (Testarray.size ()); System.out.println ("6::>" +Testarray.hashcode ()); //result:capacity = 7, size = 7; //Result:do not create a new ArrayList } Catch(Nosuchfieldexception |SecurityException e) { //TODO auto-generated Catch blockE.printstacktrace (); } Catch(IllegalArgumentException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(illegalaccessexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}

Results:

5
0
1::>1
1.0::>79
1.1::>2498
1.2::>77488
1.3::>2402179
5
4
2::>2402179
4
4
3::>2402179
4::>-1451076781
9
7
5::>-1451076781
7
7
6::>-1451076781

As you can see from the code:

ArrayList re-Creates a ArrayList object every time after appending the item until the Ensurecapacity method is applied.

Although we set the maximum length of 6, but when the number of additional content exceeds 6, with the size of the increase, the capacity of the ArrayList is larger, how much more specific to see from this example is 2, I also tried to append content, the principle of enlargement is roughly 2,3,5,8,13 ... (i = (i-1) + (i-2)).

It is also important to note that after each use of reflection to find the capacity of ArrayList to modify the ArrayList, the next time the capacity calculation needs to be re-made object Get.

Java Learning (5)--ArrayList

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.