Array and ArrayList of similarities and differences

I believe the array is the most commonly used in the program, regardless of any language there is a data structure array, because the C # language is fully object-oriented, so the array in C # is also the object
actually is an instance of the Array class, the Array class use can be said to be the most frequently used, but we did not take too much notice at the time of use, such as creating an array int [] when in fact it creates an instance of the array class object.
Recently, I carefully studied the similarities and differences between what the C # Array and ArrayList class, summed up the following points:

[Difference between Array and ArrayList of]

# 1. Array type variables must be declared at the same instantiation (available size of at least array initialization), and ArrayList may simply be declared.
Such as:
 int [] = new new Array Array [. 3];
 or int [] array = {1,2,3} ;
 or ArrayList myList = new ArrayList ();
these are legitimate, and direct use int [] array; It is not acceptable.

# 2. Array objects only store homogeneous, and ArrayList can store heterogeneous objects.
Except isomorphic objects refer to the same type of object, if declared as int [] array would only store integer data, String [] can only store character data, but the statement is the object [] array.
ArrayList and can store any of various types of data (as are being boxed objects of type Object stored inside it, in fact, is to use the internal ArrayList "object [] _items;" of such a private field of an object encapsulates)

# 3 is presented to the hosting of the CLR
Array is to always be stored in a row, while the ArrayList storage is not necessarily continuous.

# 4 initial size
initialization Array object must be given only to specify the size, and the size of the array after creation is fixed,
and you can specify the size of the ArrayList dynamic, its size can be specified during initialization, you can not specify, that the object the space can be increased arbitrarily.

# 5 Array can not arbitrarily add and remove items which, while ArrayList can insert and remove items at any position.


[Array and ArrayList of similarities]

# 1 has an index (index), which can be directly obtained by any of modifications and index.
# 2 objects they create are placed in the managed heap.
# 3 are able to enumerate themselves (because have achieved IEnumerable interface).


[ArrayList of some of the features]

# 1 in the study ArrayList I found an interesting phenomenon, capacity ArrayList property values will change over ArrayList to the actual size of the item,
the following 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();
        }

If size is 2, the output of the "current capacity" is 2,
when the size is 3 or 4, "current capacity" is 4,
when the size is 5 ~ 8, "current capacity" is 8
if size is 9 to 16, "current capacity" is 16,
...
can draw a conclusion by experiment that whenever the number (ArrayList.Count) ArrayList of objects actually present in excess of its own Capacity threshold, then the threshold is automatically doubled.
(Initial value may be changed to try to generate upon myList, but the conclusion is the same)

2 # () method may be an empty entry ArrayList instance to compress the volume removed by TrimToResize ArrayList class.
As 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();
        }

输出:
actual capacity:5
compressed capacity:3

# 3 In C # 2.0, it is recommended that you try to use generics version of the ArrayList, namely System.Collection.Generics name List <T> in space,
it will not only ensure the security type, and because there is no boxing and unboxing process, thereby increasing the efficiency of the object.

TraceBack:http://www.cnblogs.com/agassi001/archive/2006/05/31/413540.html

Reproduced in: https: //www.cnblogs.com/hdjjun/archive/2008/06/17/1223801.html

Guess you like

Origin blog.csdn.net/weixin_33670713/article/details/94497438