1.25. C # Array

发布时间 :2023-10-12 23:00:07 UTC      

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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.

Powered by Django & PostgreSQL (with PostGIS)