C # Collection interface and BitArray

Source: Internet
Author: User
Tags foreach bit set bitwise count


A Interfaces for collections in C #: ICollection



Initialization object method for the collection interface:



Icollection<data type> mycollect=new collection< Data type > ();



Now let's look at an interface for a collection of integer types



Using System.Collections.ObjectMode



You have to have this reference.
icollection<int> mycollection = new collection<int> ();



Mycollection.add (100);//Add Element



Mycollection.add (22);



Mycollection.add (30);



foreach (int x in mycollection)



Console.WriteLine (x); Output elements



Console.WriteLine ("Number of elements in the collection {0}", Mycollection.count);



Mycollection.add (222);



Mycollection.remove (22);//delete element



Console.WriteLine ("Number of elements in the collection {0}", Mycollection.count);



Console.WriteLine (Mycollection.contains (22));//Determine if there is this element in the collection





Whether an element exists in the Interpretation collection: Mycollection.contains (x); some words return 1, otherwise return 0;



Copies the elements of the collection into an array of the same size:



int[] myarray = new Int[mycollection.count];



Mycollection.copyto (myarray, 0)//starting with the first element of mycollection


int[] myarray = new Int[mycollection.count];
Mycollection.copyto (myarray, 0);

Starting with the first element of mycollection

Console.WriteLine ("Xia now compares elements in two objects");

for (int i = 0; i < Mycollection.count; i++)

{

Console.Write ("in myarray {0},", Myarray[i]);

}

foreach (int x in mycollection)

Console.WriteLine ("In Mycollection{0}", x);






You can construct other types of generic collections simply by following the methods described above.



Now demonstrate another type of generic collection:


icollection<string> another = new collection<string> ();

Constructs a collection of string types

Another. ADD ("the");

Another. ADD ("People ' s");

Another. ADD ("Republic");

Another. ADD ("the");

foreach (String str in another)

Console.Write (str);

The output result of this object is:




Two BitArray



The BitArray class is a bit array, the size of the array is determined when the object is created, each data element can only represent one bit, the value of the element can only be 1 and 0, which is 1 in true, and 0 in False, when the object is initialized with an element of another data type. Then each element will occupy the array space of the type in memory, and the following table provides a special method for this class:


Method name

The function of the method

and

Elements in BitArray perform bitwise AND operations

Or

Bitwise OR operation

Not

Take Back operations

Xor

XOR or operation

Get/set

Gets or designs the bit set at a specific location to the specified value

SetAll

Sets all the bits in the BitArray to the specified value


Initialize a BitArray class


BitArray Mybitarray = new BitArray (4);

Mybitarray[0] = false;

Mybitarray[1] = true;

MYBITARRAY[2] = true;

MYBITARRAY[3] = false;

Displaybitarray ("Mybitarray", Mybitarray);

Console.WriteLine ("After Not ()");

Mybitarray.not ();

Displaybitarray ("Mybitarray", Mybitarray);



Of course, a method is also defined to be used specifically for output:



public static void Displaybitarray (String arraylistname, BitArray Mybitarray)



{for (int i = 0; i < Mybitarray.count i++)



{



Console.WriteLine (Arraylistname + "[" + i + "] =" + mybitarray[i]);



}



}


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.