C # Arrays and collections

Source: Internet
Author: User

One-dimensional arrays

Overview: An array is defined by specifying the element type of the array, the number of dimensions of the array (rank), and the upper and lower bounds on each dimension of the array, and the definition of an array needs to contain the following elements.

The number of dimensions of the type array upper bound of each dimension

Declaration: Data Type identifier "positive integer constant";

An array type is a reference type that derives from an array of abstract base types , creates an array with the new operator, and initializes the array elements to their default values.

Declaration and use of one-dimensional arrays:

  Static one-dimensional arrays:

It is the number of exponential group elements that are fixed, that is, the amount of memory space they occupy is constant.

A one-dimensional array is a collection of one-dimensional data types that have the same data type.

The statement reads as follows:type[] arrayname;

There are a number of ways to initialize:

1. int[] arr = new int[5];

2. int[] arr = new int[5]{1,2,3,4,5};

3. string[] arrstr;

Arrstr = new striing[7]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fir", "Sat"};

Dynamic one-dimensional arrays

The declaration of a dynamic array is actually the declaration part of the array and the initialization part, respectively, written in different statements, the initialization of the dynamic array also need to use the New keyword to the array element non-matching memory space, the array element assigned an initial value.

The declaration of a dynamic one-dimensional array element is as follows:

Type [] arrayname;

Arrayname = new Type[n];

Or

type[] Arrayname = new type[];

After you declare a dynamic one-dimensional array, you can use the length property to get the total number of elements in the array, and to dynamically assign values to arrays using a for statement or a foreach statement.

Two-dimensional arrays:

Static two-dimensional arrays:

The declaration syntax is as follows:

type[,] arrayname;

Note: When you define an array of value types, its default value is 0 (including shaping, single, Double), the Boolean type defaults to false, the character array defaults to ' + ', and the string default value is null.

When declaring a two-dimensional array and initializing directly, the new keyword can be omitted, and the compiler calculates the length of the array based on the number of initial values and creates a two-dimensional array.

Dynamic two-dimensional arrays:

The declaration syntax is as follows:

type[,] arrayname;

Arrayname = new TYPE[N1,N2];

Or:

type[,] arrayname = new TYPE[N1,N2];

After declaring a dynamic two-dimensional array, you can get the number of rows and columns of a two-dimensional array by passing different parameters to the GetLength method of the Array class object to obtain each element in a two-dimensional array.

1 Static voidMain (string[] args)2         {3             /*4 int[] arr = new Int[6];5 Console.WriteLine ("Please enter a one-dimensional array:");6 for (int i = 0; i < arr. Length; i++)7             {8 Arr[i] = Convert.ToInt32 (Console.ReadLine ());9             }Ten Console.WriteLine ("Show Input array:"); One foreach (int item in arr) A             { - Console.Write ("{0}", item+ ","); -             } the Console.WriteLine (); -              */ -  -             /* + int[,] arr = new int[3, 2] {{1,1},{2,2},{3,3}}; - Console.Write ("The number of rows in the array is:"); + Console.WriteLine (arr. GetLength (0)); A Console.Write ("The number of columns in the array is:"); at Console.WriteLine (arr. GetLength (1)); - Console.Write ("\ n"); - for (int i = 0; i < arr. GetLength (0); i++) -             { - string str = ""; - For (int j = 0; J < arr. GetLength (1); J + +) in                 { - str = str + convert.tostring (arr[i, J]) + ""; to                 } + Console.Write (str); - Console.Write ("\ n"); the             } *              */ $             /*Panax Notoginseng int row = 0, col = 0; - Console.Write ("Please enter the number of rows for the two-dimensional array:"); the row = Convert.ToInt32 ((Console.ReadLine ())); + Console.Write ("Please enter the number of columns of the two-dimensional array:"); A col = Convert.ToInt32 ((Console.ReadLine ())); the  + Console.WriteLine ("Please enter the value of the two-dimensional array:"); - int[,] arr1 = new Int[row, col]; $ for (int i = 0; i < arr1. GetLength (0); i++) $             { - For (int j = 0; J <arr1. GetLength (1); J + +) -                 { the arr1[i, j] = Convert.ToInt32 ((Console.ReadLine ())); -                 }Wuyi             } the  - //Display Wu Console.WriteLine ("Show input two-dimensional array:"); - for (int i = 0; i < arr1. GetLength (0); i++) About             { $ For (int j = 0; J < arr1. GetLength (1); J + +) -                 { - Console.Write (arr1[i,j]+ ""); -                 } A Console.WriteLine (); +             } the              */ -              $             /* the //Bubble sort the int[] arr = new int[]{1,4,3,5,7,2,6,10,9,8}; the int temp; the Console.Write ("before sorting:"); - foreach (int item in arr) in             { the Console.Write (item+ ""); the             } About Console.WriteLine (); the Console.Write ("After sorting:"); the for (int i = 0; i < arr. Length-1; i++) the             { + For (int j = i+1; J < arr. Length; J + +) -                 { the if (Arr[i] > arr[j])Bayi                     {  the temp = arr[j]; the Arr[j] = arr[i]; - Arr[i] = temp; -                     } the                 } the             } the foreach (int item in arr) the             { - Console.Write (item+ ""); the             } the              */ the             94             /* the //Select sort the int[] arr = new int[] {10,2,3,6,4,9,8,1,5,7}; the Console.Write ("before sorting:");98 foreach (int item in arr) About             { - Console.Write (item+ "");101             }102 Console.WriteLine ();103 Console.Write ("After sorting:");104 int min; the for (int i = 0; i < arr. Length-1; i++)106             {107 min = i;108 For (int j = i+1; J < arr. Length; J + +)109                 { the if (Arr[min] > arr[j])111 min = j; the                 }113 int temp = arr[i]; the Arr[i] = arr[min]; the Arr[min] = temp; the             }117 foreach (int item in arr)118             {119 Console.Write (item+ ""); -             }121              */122             123 Console.WriteLine ();124 console.readline (); the}
View Code

ArrayList Collection class:

The ArrayList collection , located under the System.Collections namespace, can dynamically add and remove elements and is a non-generic collection class. ArrayList is equivalent to an advanced dynamic array, which is an upgraded version of the array class, but it is not equivalent to an array.

ArrayList class overview:

1, the capacity of the array is fixed, and ArrayList capacity can be automatically expanded as needed

2. ArrayList provides a way to add, delete, and insert a range element, but to get or set the value of an element once in the array.

3. ArrayList will provide read-only and fixed-size wrapping methods that are returned to the collection, and arrays are not provided.

4,ArrayList is only one-dimensional form, and the array can be multidimensional.

The properties are as follows:

Capacity

Gets or sets the number of elements that ArrayList can contain.

Count

Gets the number of elements actually contained in the ArrayList .

Isfixedsize

Gets a value that indicates whether the ArrayList has a fixed size.

IsReadOnly

Gets a value that indicates whether ArrayList is read-only.

IsSynchronized

Gets a value that indicates whether access to the ArrayList is synchronized (thread safe).

Item

Gets or sets the element at the specified index.

SyncRoot

Gets an object that can be used to synchronize ArrayList access.

Method:

Add to:

public virtual int Add (object value);

public virtual void Insert (int index,object value);

Delete:

public virtual void Clear (); Clear all elements

public virtual void Remove (); To remove the first occurrence of a specific object

public virtual void RemoveAt (int index);//Remove the element at the specified index

public virtual void RemoveRange (int index,int count);//Remove the element at the specified index

Index: Start index

Count: The number of elements to be removed

C # Arrays and collections

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.