Simple implementation and difference between IList and ArrayList

Source: Internet
Author: User

Simple implementation and difference between ilist and arraylist
First, I read a book and mentioned that ilist is faster than arraylist.

// Test the list container Function

Void main (void)
{
// The list1 object is empty.
Intlist list1;
// The list2 object initially has 10 elements with a value of 6.
Intlist list2 (10, 6 );
// The list3 object initially has three elements with a value of 6.
Intlist list3 (list2.begin (), -- list2.end ());

// Declare a bidirectional iterator named I
Intlist: iterator I;

// Display the elements of each list object from the front to the back
Put_list (list1, "list1 ");
Put_list (list2, "list2 ");
Put_list (list3, "list3 ");

// Add two elements after the list1 Sequence
List1.push _ back (2 );
List1.push _ back (4 );
Cout <"list1.push _ back (2) and list1.push _ back (4):" <endl;
Put_list (list1, "list1 ");

// Add two elements from the front of the list1 Sequence
List1.push _ front (5 );
List1.push _ front (7 );
Cout <"list1.push _ front (5) and list1.push _ front (7):" <endl;
Put_list (list1, "list1 ");

// Insert data in the middle of the list1 Sequence
List1.insert (++ list1.begin (), 3,9 );
Cout <"list1.insert (list1.begin () + 1, 3, 9):" <endl;
Put_list (list1, "list1 ");

// Test the reference class function
Cout <"list1.front () =" <list1.front () <endl;
Cout <"list1.back () =" <list1.back () <endl;

// Remove an element from the beginning and end of the list1 Sequence
List1.pop _ front ();
List1.pop _ back ();
Cout <"list1.pop _ front () and list1.pop _ back ():" <endl;
Put_list (list1, "list1 ");

// Clear the 2nd elements in list1
List1.erase (++ list1.begin ());
Cout <"list1.erase (++ list1.begin ():" <endl;
Put_list (list1, "list1 ");

// Assign a value to list2 and display it
List2.assign (8, 1 );
Cout <"list2.assign (8, 1):" <endl;
Put_list (list2, "list2 ");

// Display the sequence status information
Cout <"list1.max _ size ():" <list1.max _ size () <endl;
Cout <"list1.size ():" <list1.size () <endl;
Cout <"list1.empty ():" <list1.empty () <endl;

// List sequence container operation
Put_list (list1, "list1 ");
Put_list (list3, "list3 ");
Cout <"list1> list3:" <(list1> list3) <endl;
Cout <"list1 <list3:" <(list1 <list3) <endl;

// Sort the list1 container
List1.sort ();
Put_list (list1, "list1 ");

// Combined processing
List1.splice (++ list1.begin (), list3 );
Put_list (list1, "list1 ");
Put_list (list3, "list3 ");
}


, How to use arraylist
The simplest example:


Arraylist list = new arraylist ();
For (int I = 0; I <10; I ++) // Add 10 int elements to the array.
List. add (I );
//... The program does some processing
List. removeat (5); // remove the 6th Elements
For (int I = 0; I <3; I ++) // Add three more elements.
List. add (I + 20 );
Int32 [] values = (int32 []) list. toarray (typeof (int32); // returns the array contained in arraylist

This is a simple example. Although it does not contain all the methods of arraylist, it can reflect the most common usage of arraylist.


3. Important arraylist methods and attributes
(1) constructor
Arraylist provides three constructors:
Public arraylist ();
The default constructor initializes the internal array with the default size (16 ).
Public arraylist (icollection );
Construct with an icollection object and add the elements of the set to arraylist
Public arraylist (int );
Initializes an internal array with the specified size.
(2) issynchronized attributes and arraylist. synchronized Method
The issynchronized attribute indicates whether the current arraylist instance supports thread synchronization, while the static method of arraylist. synchronized returns the encapsulation of a thread synchronization of arraylist.
If a non-thread synchronization instance is used, you need to manually call lock to maintain thread synchronization during multi-thread access. For example:


Arraylist list = new arraylist ();
//...
Lock (list. syncroot) // when arraylist is not packaged in a thread, the syncroot attribute is actually its own. However, to meet the syncroot definition of icollection, syncroot is used to maintain the standardization of source code.
...{
List. add ("add a item ");
}

If you use arraylist. the instance returned by the synchronized method does not need to be considered for thread synchronization. This instance itself is thread-safe. In fact, arraylist implements an internal class to ensure thread synchronization, arraylist. synchronized returns an instance of this class. Every attribute in it uses the lock keyword to ensure thread synchronization.
****
However, use this method (arraylist. synchronized) does not guarantee the synchronization of enumeration. For example, if one thread is deleting or adding collection items, and the other thread is enumerating at the same time, enumeration will throw an exception. Therefore, you must use syncroot to lock the set during enumeration.
 
The usage of hashtable and arraylist on thread security is similar.

The definitions of list and arraylist are provided in the annotations. It can be seen that they are similar. Arraylist only has icloneable while several generic interfaces are missing.

In the subsequent code, use the add method to add int type data to the list, and then enumerate the data in the foreach form. Note that! There is a problem with the enumeration part of the code, which we will mention in (below.

We also recommend a very good tool ilspy developed by sharpdevelop. We strongly recommend that dotnetprogrammers download and use it.

I listed the c # code and il code from ilspy disassemble in the back. Note that for the foreach Statement of arraylist, the c # code differs from the source code (lines 79 to 96). The Compiler adds an ienumerator enumerator2 = cfromarraylist. getenumerator (); local variable.

In addition, use int num5 = (int) enumerator2.current; to access iterator. In addition, the finally part of idisposable is added.

Let's continue to look at the il code section. For the list form, the il Code does not have the box Packing Instruction, while the arraylist has a box instruction in line 145, which is one of the performance differences.
But the strange thing is that in the enumeration part, the il code generated by ilspy (and ildasm) is basically the same for arraylist and list, there are also calls to movenext, current, and idisposable interfaces.

The arraylist only has unbox and box commands.

As expected, the running result is much faster than the arraylist.

 

Related Article

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.