1.25. C # Array

发布时间 : 2025-10-25 13:34:37 UTC      

Page Views: 10 views

An array is a sequential collection of fixed sizes that stores elements of the same type. An array is a collection used to store data, and an array is generally thought of as a collection of variables of the same type.

Declaring an array variable is not a declaration number0 number1 、…、 number99 individual variables, but declare one like numbers such a variable, and then use the numbers[0] numbers[1] 、…、 numbers[99] to represent individual variables. A specified element in the array is accessed by index.

All arrays are made up of consecutive memory locations. The lowest address corresponds to the first element, and the highest address corresponds to thelast element.

1.25.1. Declare array #

To declare an array in C #, you can use the following syntax:

datatype[] arrayName; 

Among them

  • datatype use to specify the type of element stored in the array.

  • [ ] specifies the rank (dimension) of the array. Rank specifies the sizeof the array.

  • arrayName specifies the name of the array.

For example:

double[] balance; 

1.25.2. Initialize array #

Declaring an array does not initialize the array in memory. When you initialize an array variable, you can assign values to the array.

Array is a reference type, so you need to use the new keyword to createan instance of the array.

For example:

double[] balance = new double[10]; 

1.25.3. Assign values to an array #

You can assign values to a single array element by using index numbers, suchas:

double[] balance = new double[10]; balance[0] = 4500.0; 

You can assign values to an array while declaring it, such as:

double[] balance = { 2340.0, 4523.69, 3421.0}; 

You can also create and initialize an array, such as:

int [] marks = new int[5] { 99, 98, 92, 97, 95}; 

In the above cases, you can also omit the size of the array, such as:

int [] marks = new int[] { 99, 98, 92, 97, 95}; 

You can also assign an array variable to another target array variable. In this case, the destination and source point to the same memory location:

int [] marks = new int[] { 99, 98, 92, 97, 95}; int[] score = marks; 

When you create an array, the C# compiler implicitly initializes each array element to a default value based on the array type. For example, all elements of the int array are initialized to 0 .

1.25.4. Access array elements #

The element is accessed through the indexed array name. This is achieved by placing the index of the element in square brackets after the array name. For example:

double salary = balance[9]; 

Here is an example that uses the three concepts mentioned above, namely, declare, assign, and access arrays:

Example #

using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; /* n is an array with 10 integers */ int i,j; /* Initialize elements in array n */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; } /* Output the value of each array element */ for (j = 0; j < 10; j++ ) { Console.WriteLine("Element[{0}] = {1}", j, n[j]); } Console.ReadKey(); } } } 

When the above code is compiled and executed, it produces the following results:

Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109 

1.25.5. Use foreach Cycle #

In the previous example, we used a for loop to access each array element. You can also use a foreach statement to traverse the array.

Example #

using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; /* n is an array with 10 integers */ /* initialize elements in array n */ for ( int i = 0; i < 10; i++ ) { n[i] = i + 100; } /* Output the value of each array element */ foreach (int j in n ) { int i = j-100; Console.WriteLine("Element[{0}] = {1}", i, j); } Console.ReadKey(); } } } 

When the above code is compiled and executed, it produces the following results:

Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109 

1.25.6. C # array details #

In C #, arrays are very important and you need to know more details. Here are some important array-related concepts that C # programmers must be awareof:

Concept

Description

Multidimensional array

C # supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array.

Staggered array

C # supports staggered arrays, that is, arrays of arrays.

Pass an array to a function

You can pass a pointer to the array to the function by specifying the name of the array without an index.

Parameter array

This is usually used to pass an unknown number of arguments to a function.

Array class

Defined in the System namespace, is the base class for all arrays and provides a variety of properties and methods for arrays.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。