Array definition, initialization and its properties

Source: Internet
Author: User
Tags array definition array sort

One-dimensional array definition: int[] Myar;

one-dimensional array declarations and their initialization:

Declare the array and set the length of the array: int[] myarr=new int[] (5);

If you initialize an array with curly braces, you can also not specify the size of the array, because the compiler automatically counts the number of elements: int[] myarr=new int[]{4,7, 11, 2};

There is also a more streamlined form of using C # translators. Jiao can declare and initialize an array at the same time with curly braces: int[] MyArray = {4, 7, 11, 2};

Iterate through elements in a group:
for (int i = 0 San Zhi i < myarr.length; i++)
{
Console.writel⊥ne (Myarray[il]);
}

foreach (Var val in Myaarr)

{
Console.writel⊥ne (Myarray[il]);
}

Multidimensional Array Definitions:

int[,] arr=new int[3,3] ();//definition and initialization of multidimensional arrays

Multidimensional Array Assignment:

Arr[0,0]=1;

arr[0,1]=2;

arr[0,2]=3;

arr[1,0]=4;

arr[1,1]=5;

arr[1,2]=6;

arr[2,0]=7;

arr[2,1]=8;

arr[2,2]=9;

Simplified way to define arrays and initialize and assign values: int[,] arr={{1,2,3},{4,5,6},{7, 8, 9}}

Array value assignment and the acquiring party Array Intarray =array.createinstance (typeof (int) 5) for (int i = 0; I <5; i++{

Intarray.setvalue (, i);
}

For (int i = 0;i<5; i++)
{
Console.WriteLine (Intarray1.getvaue (i));

}

Array replication:

int intArray2 = (int[]) intarry1.clone ();

In addition to using the clone () method, you can create a shallow copy using the fine feather four Ⅱ⑽ method. But the clone method and the Copy party
Method has one important difference: Theclone method creates a new array, and the copy method must pass the same order and have sufficient elements of its own
There are arrays.

Array sort: array.sort ();

Array definition, initialization and its properties

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.