C # basic knowledge sorting: Basic knowledge (14) array

Source: Internet
Author: User

Regardless of the language, there must be a set of concepts. The simplest and most intuitive set should be an array, which is a continuous space in the memory. Look at the array in C #.

.
1. Int [] intarry;
Intarry = new int [6];
An int array variable intarry is declared here, and an int array object with six units is saved;
Int [,] intarry2 = new int [3, 4];
Declare an int two-dimensional array variable and initialize an array object with three rows and four columns;
Int [] [] intarry3 = new int [9] [];
Declares an array variable of the int array type. Each array element is an int array object reference.
Because it is an object-oriented language, reference and object are mentioned above. Actually:
1. the. NET frameword array is not a simple data structure, but a type called an array;
2. the array variables in. NET Framework Save the reference to the array type object, that is, the array is an object.
All. NET Framework arrays (INT [], string [], object []) are subclasses inherited from array. Generally, the array class is not directly used because

Of course, it also includes C #, which maps array objects to their own special syntax, such as int [], string [].
An array has two attributes: Index and length, that is, index and length. An index is used to access elements in an array object. The length is the length of an array.
Take a look at the contact code:

Public class myarray {// <summary> // define an array for testing /// </Summary> Public void testint () {int [] intarry1 = NULL; intarry1 = new int [6]; int [,] intarry2 = new int [3, 4]; int [] [] intarry3 = new int [9] [];} /// <summary> /// test the conversion from a value-type array to a reference-type array /// </Summary> /// <Param name = "array"> </param>/ // <returns> </returns> Public static object [] int32toarrayofobject (INT [] array) {object [] objarray = new object [array. length]; for (INT I = 0; I <array. length; I ++) {objarray [I] = array [I];} return objarray ;} /// <summary> /// test the main features of the array /// </Summary> Public static void maintest () {// declare a string-type array string [] sarray = new string [10]; // access the array // assign a value for (INT I = 0; I <sarray. length; I ++) {sarray [I] = @ "string" + I;} consoletoclientstring (sarray); // another way to declare an array, the so-called enumeration method sarray = new string [] {"teststring0", "teststring1", "teststring2"}; consoletoclientstring (sarray); // The array copies string [] newsarray = sarray. clone () as string []; consoletoclientstring (newsarray); // use the createinstance method of array to declare the integer array int [] intarray = array of 10 elements. createinstance (typeof (INT), 10) as int []; for (INT I = 0; I <intarray. length; I ++) {intarray [I] = I;} leletoclientint (intarray); // copy between arrays, specify the location, specify the length of int [] newintarray = new int [20]; array. copy (intarray, 3, newintarray, 4, intarray. length-3); consoletoclientint (newintarray); object [] objarray = sarray; consoletoclientobject (objarray); objarray = inline (intarray); consoletoclientobject (objarray ); // array int [] [] intarrayarray = new int [9] []; console. writeline ("array length:" + intarrayarray. length); // assign a value for (INT I = 1; I <10; I ++) {intarrayarray [I-1] = new int [I]; for (Int J = 1; j <= I; j ++) {intarrayarray [I-1] [J-1] = I * j ;}} consoletoclientarrayarrayint (intarrayarray ); // two-dimensional array int [,] intarray2d = new int [9, 9]; console. writeline (string. format ("two-dimensional array length: {0}, dimension: {1} * {2}", intarray2d. length, intarray2d. getlength (0), intarray2d. getlength (1); For (INT I = 1; I <10; I ++) {for (Int J = 1; j <= I; j ++) {intarray2d [I-1, J-1] = I * j ;}} int COUNT = 0; foreach (INT item in intarray2d) {If (item> 0) {console. write ("{0, 2}", item);} If (++ count> = 9) {console. writeline (); Count = 0 ;}} static void consoletoclientarrayarrayint (INT [] [] intarrayarray) {foreach (INT [] Item1 in intarrayarray) {foreach (INT item2 in Item1) {console. write ("{0, 2}", item2);} console. writeline ();} console. writeline ();} static void consoletoclientstring (string [] sarray) {foreach (string item in sarray) {console. write (item + @ ",");} console. writeline ();} static void consoletoclientint (INT [] intarray) {foreach (INT item in intarray) {console. write (item + @ ",");} console. writeline ();} static void consoletoclientobject (object [] objarray) {foreach (Object item in objarray) {console. write (item. tostring () + @ ",");} console. writeline ();}}

Call

    class Program    {        static void Main(string[] args)        {            MyArray.MainTest();            Console.ReadLine();        }    }

Result


You can know from the above:
Arrays have arrays of reference type and value type. For arrays of reference type, elements are used to save references of objects. The initialization value is null. For arrays of value type, elements are saved.

Object value. The initialization value for numeric type is 0.
Arrays have dimensions, but multi-dimensional arrays and arrays are different. intarrayarray and intarray2d are different. An array of M * n represents the deciding factor.

Multi-dimensional array, each element is an array of array objects.
Arrays, like other collection classes, implement the icollection interface and all have enumeration and iteration functions.

Code download: http://download.csdn.net/detail/yysyangyangyangshan/4640445

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.