C # one-dimensional array (2)

Source: Internet
Author: User

An array is a common type of data and is a reference type. It is made up of an array of elements of the same data type. In the C # language type system, arrays are derived from abstract class System.Array. In memory, the array occupies a contiguous amount of memory, the elements are sequentially stored together, and each individual element in the array does not have its own name, but can be accessed or modified by its location (index).

In C #, the index of an array element starts with 0, that is, for an array of N elements, the index range is from 0~n-1.

Here is a discussion of how an array is used.

1. Declaration of one-dimensional arrays

When an array is declared, the primary declaration is the name of the array and the type of element that is contained, in the following general format:
Array type [] array name;

Array type, which can make any valid data type in C #, including classes; The array name can be any valid identifier in C #. Here are a few examples of array declarations:

int[] num;
float [] fnum;
String[] SWords;
Studnet[] Stu; Student is a well-defined class type

Attention:

The data type [] is an array type, and the variable name is placed after [], which is different from C and C + +;

You cannot specify a length when declaring an array.

2. Creation of one-dimensional arrays

Creating an array is allocating memory to the array object. Because the array itself is also a class, as with classes, when declaring an array, the array is not actually created, and the array object is created with the new operator before use. There are several ways to create the method:

(1) Declare first, then create

The form is:

data type [] array name;
Array name = new data type [number of elements];

Here are a few examples:

int [] num;        num = new INT[10]; Declares and creates an array num with 10 integer elements.
String[] STR;     str = new STRING[3]; Declares and creates an array of 3 string data types str.
double [] dnum;  Dnum = new Double[5]; Declares and creates an array dnum with 5 data elements of type double.

(2) Creating an array at the same time as the declaration

data type [] Array name = new data type [number of elements];

Here are a few examples:

int[] num = new INT[10];
double[] t = new double[4];
short[] st = new SHORT[17];

3. Initialization of one-dimensional arrays

The array is defined with the value of the element given, that is, the initialization of the array, with the following initialization methods:

(1) data type [] Array name = new data type [number of elements]{initial value list}; As in the following example:

int [] num = new int[4]{12,34,56,78};
string[] str = new string[3]{"You", "and", "Me"};
Float[] f = new float[5]{1.345f,12,13.5f,109.345f,12.1f};

(2) Omit the size of the array, that is: data type [] Array name = new data type []{initial value list}; As in the following example:

short[] st = new short[]{2,4,67,3}; The number of array elements is 4
int[] INum = new int[]{23,45,67,89,100,234,567,234}; The number of array elements is 8

(3) Further omitting new and data type [], that is: data type [] Array name = {initial value list}; As in the following example:

String[] names = {"Wangtao", "Liuli", "Sanmao", "Shanghaitan", "Jinghuayanyun"};
int [] INum = {45,28,34,74,84};

4. Assignment of one-dimensional arrays

To assign a value to an array, you need to use the index of the array, in the following format:

array name [index value] = value of the data;

Let's try an example:

int [] A = new int[4];

A[0] = 24;
A[1] = 54;
A[2] = 87;
A[3] = 93;

Note that the above example index is from 0~3. We also see that when assigning a large number of arrays, it becomes more cumbersome and less convenient to initialize.

5. Common properties and methods for array classes

Length gets the number of array elements

Rank gets the rank (number of dimensions) of the array, and rank is always 1 for a one-dimensional array.

GetLength (int) Gets the number of elements for the specified dimension.

6. Examples of array applications

(1) The 10 numbers are sorted by the selection method by small to large.

The source program code is as follows:

Using System;

Class Class1
{
static void Sort (int[] a)
{
int i,j,k,temp;
for (i=0;i<a.length-1;i++)
{
K=i;
for (j=i+1;j<a.length;j++)
if (A[k]>a[j])
K=j;
if (k!=i)
{temp = A[i];a[i] = A[k]; a[k] = temp;}
}
}
static void Main (string[] args)
{
int[] AA = new int[]{918,124,58,1125,825,51,82};
Sort (AA);
Console.WriteLine ("Sorted Result:");
for (int i=0;i<aa. length;i++)
Console.Write (aa[i]+ "");
Console.WriteLine ();
}
}

The results of the operation are as follows:

(2) Find the largest element in an integer array and its index value.

The source program code is as follows:

The results of the operation are as follows:

C # one-dimensional array (2)

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.