Similarities and differences between array and arraylist

Source: Internet
Author: User

I believe that arrays are the most commonly used in programming. No matter how many languages have data structures like arrays, C # is fully object-oriented, therefore, arrays in C # are also objects,
Actually, it is an example of the array class. The use of the array class can be said to be the most frequent, but we don't care much about it when using it, for example, an instance of an array object is actually created when an array int [] is created.
Recently, I have carefully studied the similarities and differences between the array and arraylist classes in C #, and summarized the following points:

[Differences between array and arraylist]

#1. variables of the array type must be instantiated at the same time (at least the size of the array must be initialized), while arraylist can only be declared first.
For example:
Int [] array = new array [3];
Or int [] array = {1, 2, 3 };
Or arraylist mylist = new arraylist ();
These are legal, but using int [] array directly cannot.

#2. Arrays can only store homogeneous objects, while arraylist can store heterogeneous objects.
Homogeneous objects refer to the objects of the same type. If an array declared as int [], only integer data can be stored, and string [] can only be stored in plain data, except for arrays declared as object.
The arraylist can store any different types of data (because it stores packed object objects, in fact, the arraylist uses "object [] _ items; "Such a private field to encapsulate the object)

#3 storage method in CLR hosting pair
Array is always stored continuously, and the storage of arraylist is not necessarily consecutive.

#4 initialization size
The array object initialization must only specify the size, and the size of the created array is fixed,
The size of the arraylist can be dynamically specified. The size can be specified during initialization or not, that is, the space of the object can be increased at will.

#5 arrays cannot add or delete items at will, while arraylist can insert or delete items anywhere.


[Similarity between array and arraylist]

#1 all have indexes, that is, you can use indexes to directly obtain and modify any items.
#2 all the objects they created are stored in the managed heap.
#3 can enumerate itself (because both implement the ienumerable interface ).


[Features of arraylist]

#1 I found an interesting phenomenon in studying arraylist. The capacity attribute value of arraylist changes with the actual size of the items in arraylist,
As follows:Code:

Public   Static   Void Main ( String [] ARGs)
{
Arraylist mylist =   New Arraylist ( 2 );
Console. writeline ( " Initial capacity: "   + Mylist. capacity );

Int Size =   2 ;
For ( Int I =   0 ; I < Size; I ++ )
{
Mylist. Add (I );
}
Console. writeline ( " Current capacity: "   + Mylist. capacity );

Console. Readline ();
}

When the size is 2, the "current capacity" in the output result is 2,
When the size is 3 or 4, "current capacity" is 4,
When the size is 5 ~ "Current capacity" is 8,
When the size is 9 ~ When 16, "current capacity" is 16,
...
Through experiments, we can draw a conclusion that this threshold will automatically double whenever the actual number of objects (arraylist. Count) in the arraylist exceeds its own capacity threshold.
(You can also change the initial value of mylist generation to try it, but the conclusion is the same)

#2 use the trimtoresize () method of the arraylist class to divide the null items in the arraylist instance by the compressed volume.
Run the following code:

Public   Static   Void Main ( String [] ARGs)
{
Arraylist mylist =   New Arraylist ( 5 );

For ( Int I =   0 ; I <   3 ; I ++ )
{
Mylist. Add (I );
}
Console. writeline ( " Actual capacity: "   + Mylist. capacity );
Mylist. trimtosize ();
Console. writeline ( " Compressed capacity: "   + Mylist. capacity );

Console. Readline ();
}

Output:
Actual capacity: 5
Compressed capacity: 3

#3 in C #2.0, we recommend that you use the standard version of arraylist, that is, the list in the namespace of system. collection. Generics <t>,
This not only ensures the type security, but also improves the object processing efficiency because there is no packing or unpacking process.

 

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.