The usage and conversion of array and ArrayList in ASP.net C #

Source: Internet
Author: User
Tags arrays instance method int size

1. array-Array

The method for defining an array in C # is generally:

Data Type [] array=new DataType [Size];

Where the data type can be a basic data type and an object type, the size is the number of array elements.

Example defines an int type of data to represent the number of people in a class

int [] classnum=new int[20];


Type[] Typename=new type[size];
Such as

Int[] a=new int[2];string[] str=new string[5];

In fact, we usually use int[],string[] ... , we've created an array of arrays, but we don't usually have that kind of consciousness.

(1): Type data types can not be missing, and to unify, and can not be as int[] a=new array[];

(2): The size of the array can not be missing, or C # is considered an error, because the array is a fixed length of memory;

(3): The right side is a bracket [], not ()

The method of array object has static method and instance method;

public static int BinarySearch (Array A, object v);

Array a must be one-dimensional and orderly; The result of this method is to return the index number of the first element in a that matches the V. If there is no matching element, return a negative 1;

public static int BinarySearch (Array A, object V, IComparer comp);

Similar to the previous method, the difference is comp; used to compare symbols;

public static int BinarySearch (Array A, int start,int count, Object v)

Queries an index number of the element that matches v from the Count element in start of a.

public static int BinarySearch (Array A, int start,int count, Object V,icomparer comp)

Similar to the previous method. The difference is to compensate by comparison;

public static void Clear (Array A, int start, int count)

Place 0 from the Count element in array a starting from start;

Public virtual Object Clone () copy this array;

public static void Copy (Array source,array dest, int count)

Assigns the count elements in source to the array dest.

public virtual void CopyTo (Array dest, int start)

Assigns the start element in the array object that called this method to Dest.

public static Array CreateInstance (Type t, int size)

Creates an array with a size element, where the element type is t;

public static Array CreateInstance (Type t, int size1, int size2)

Creates a two-dimensional array whose arguments are [size1][size2]; the type of the array element is t;

public static Array CreateInstance (Type t, int size1,int size2, int size3)

Creates a three-dimensional array, the element type is T, the same as creating a two-dimensional array.;

public static Array CreateInstance (Type T, int[] sizes)

Creates an array of parameter sizes dimensions, the element type is t;

public static Array CreateInstance (Type T, int[] sizes,int[] startindexes)

Creates a multidimensional array in which the array dimension is sizes, and the data type of each element is T. The start index number of each dimension array in the array is startindexes;

public override bool Equals (Object v)

Determines whether V is the same as the array object that called this method.

Public virtual IEnumerator GetEnumerator ()

Returns an enumeration object for an array

public int GetLength (int dim)

Returns the number of elements in the dim dimension of a multidimensional array

public override int GetHashCode ()

Returns the hash code of the array;

public int GetUpperBound (int dim)

Returns the last index value of the element in the dim dimension of a multidimensional array

public object GetValue (int index) Returns the value of the index element of a one-dimensional array

The public object GetValue (int idx1, int idx2) and public object GetValue (int idx1, int idx2,int idx3) Get the value of a particular element of the 2-and 3-D array respectively.

The public static int IndexOf (Array A, object v) returns the index value of the first element in a that is the same as the value of V;

public static int IndexOf (Array A, object V,int start)

Starts with the start element in a, returning the element position as the value of V;

public static int IndexOf (Array A, object v,int start, int count)

Returns the same element position as the value of V from the Count element starting with the start element in A;

public static int LastIndexOf (Array A, object v,int start, int count)

Find the index number of the element in the count element that is the same as the V in a that starts with the first element in a

The public static void Reverse (array a) reverses all elements of array A;

The public void SetValue (object v, int index) sets the value of the index element of the array object to V;

Object two-and three-dimensional arrays have similar functions; public void SetValue (object v, int idx1, int idx2) and public void SetValue (object v, int idx1,int idx2, int i DX3)

The public static void sort (array a) is sorted by array A;

Two. Define and initialize an array

Int[] Myintarray;     Myintarray = new Int[4]; Using the function indexof () and LastIndexOf () of the array to get the position of a particular element in the array;

The code is as follows Copy Code

Int[] Intarray = {1, 2, 1, 3};
Console.WriteLine ("Intarray:");

for (int i = 0; i < intarray.length; i++)

{

Console.WriteLine ("intarray[" + i + "] =" +

Intarray[i]);}

int index = Array.indexof (Intarray, 1);

Console.WriteLine ("Array.indexof (intarray, 1) =" + index);

index = Array.lastindexof (Intarray, 1);

Console.WriteLine ("Array.lastindexof (intarray, 1) =" + index);

Second, C # ArrayList array usage Examples:

The code is as follows Copy Code

ArrayList al = new ArrayList ();
for (int i = 0; i < 3; i++)
{
Al. ADD (i);
Response.Write (Al[i]. ToString () + "<br>");//The element value in the output array
Response.Write (al[i]+ "<br>");
}
Response.Write (al. Count + "<br><br>");

foreach (int obj in AL)//or foreach (Object obj in AL) because Al is an array of type Object
{
Response.Write (obj+ "-ok" + "<br>");
}


ArrayList uses enumerator to access arrays:

The code is as follows Copy Code

ArrayList list = new ArrayList (1);

for (int i = 0; i < i++)

List. ADD (i);

IEnumerator ETR = list. GetEnumerator ();//Enumeration

while (ETR. MoveNext ())

Console.Write (ETR. Current + "");

Run Result:

By the right, if you want to use ArrayList in C #, you must include a reference in the System namespace, or there will be an error. Using System.Collections;

Here are some common methods for dynamic arrays:

  code is as follows copy code

ArrayList list = new ArrayList (5);

//construct a dynamic array

//list. Clear ();//clearly all elements in the array

Console.WriteLine ();

Console.WriteLine ("There are {0} elements in the list", list. Count);

ArrayList Shuzu = new ArrayList (5);

Shuzu. ADD ("Paladin One");

Shuzu. Add ("Liang Jian");

Shuzu. ADD ("Bianceng.");

Shuzu. ADD ("National Treasures");

Shuzu. ADD ("Lu Xiaofeng");

for (int x = 0; x <= shuzu. Count-1; x + +)

Console.WriteLine (Shuzu[x]);

Console.WriteLine ("Now reverse ITN");

Shuzu. Reverse ();

Console.WriteLine ("After Reversern");

Console.WriteLine ();

for (int x = 0; x <= shuzu. Count-1; x + +)

Console.WriteLine (Shuzu[x]);


Three, the transformation between ArrayList and array

(1) The following is an instance usage that copies the values in the ArrayList array to the array

The code is as follows Copy Code

Int[] Iresult=new Int[al. Count];
Al. CopyTo (Iresult);
Or use the following method
Int[] Iresult = (int[]) al. ToArray (typeof (Int32));//toarray (Int32); This error, be sure to force type conversion
Person[] Personarrayfromlist = (person[]) Personlist.toarray (typeof (person));

foreach (int item in Iresult)
{
Response.Write (item. ToString ());
}

Response.Write ("" + "<br>" + "result is:<br>") is used to copy the value of the array in array to ArrayList.

              int[] A ={222, 333, 555};
            ArrayList arrlist = new ArrayList ();
            foreach (Object obj in a)//or foreach (int obj in A)
            {
                 arrlist.add (obj);
                Response.Write ( obj+ "<br>");
           }

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.