C # notes, arraylist iteration errors and struct iteration errors

Source: Internet
Author: User

This is one of the two major problems I encountered this week.
Arraylist is a dynamic array class provided by C #. Its usage is similar to STD: list, but its core structure is the array class of C #, so it inherits most of the Methods of array.
If you have used iterative methods to delete elements in the array, for example:
Foreach (INT sub in m_listtemparray)
{
If (sub = 1) m_listtemparray.remove (sub );
}
So it will certainly pass during compilation, because it seems that there is no problem, but an exception may be thrown during actual execution.
The reason is actually very simple. In the iteration process, you remove one of the elements, resulting in a change to the entire list structure, but the iterator is not reset and still iterated according to the old structure, therefore, an exception may be thrown.
Solution 1: clone a temporary Array
Arraylist m_listclonearray = (arraylist) m_listtemparray.clone ();
Foreach (INT sub in m_listclonearray)
{
If (sub = 1) m_listtemparray.remove (sub );
}
The principle is very simple. We cloned an array for iteration. In this way, removing only changes the structure of the original array, but iteration is performed in the cloned array, so no exception occurs, however, a fatal weakness of this method is that a new arraylist needs to be constructed. If it is used in the logic of frequent update operations, efficiency will definitely be affected.
Solution 2: Do not use the iterator
For (INT I = 0; I <m_listtemparray.count ;)
{
If (m_listtemparray [I] = 1)
{
M_listtemparray.removeat (I );
}
Else
{
I ++;
}
}
One advantage of using the most primitive for loop traversal is that after each loop ends, the condition will be re-determined, even if the Count changes, so this will not cause exceptions ~ The disadvantage is that the structure is not rigorous, but the efficiency is very high. It depends on your choice ~
Solution 3: arraylist is not practical
This is a method of pulling, but I still recommend that you do not use arraylist frequently. In fact, C # provides a better container class called hashtable, which is a hash table. In addition to a key value, the usage is not much different from that of arraylist. However, due to the special data structure of the hash table, there is no arraylist problem during iterative deletion. When an object is accessed using the key value, the speed of the hash table is significantly higher than that of the arraylist
Then let's talk about struct, that is, struct.
The struct in C # is not generally weak.
As we all know, C # all objects are derived from the object class, and the object class is derived from two categories, value type and reference type. All struct values are derived from the value type, and all classes are derived from the reference type.
The Value Type of C # cannot have a default no-argument constructor. That is to say, You Cannot initialize members in the default constructor of the struct. If you write the following struct
Struct {
Int;
Arraylist B;
Int [] C;
}
Then you only need to initialize those Members when calling them, such
A;
A. B = new arraylist ();
A. c = new int [10];
If you do not do this, the B and C members of object a are both null values, which are inaccessible and not easy to use.
In addition, if you store a struct object in the array, you may encounter problems during foreach, that is, the value of the struct object cannot be modified during iteration!
Solution? So there is no need for struct. Instead, I will replace struct with class. All the problems are solved. Is there a no-argument constructor? No problem. Initialize members? No problem. Modify members during iteration? Of course! Therefore, in C #, it is really difficult to use struct unless it is simple to the data of dummies and idiots.

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.