首页  >  文章  >  后端开发  >  C# 中的数组

C# 中的数组

王林
王林原创
2024-09-03 15:11:33378浏览

数组是一组对象。数组中存在的元素具有相同的数据类型。可能是int、float、char等。C#中数组的概念应运而生,就是为了避免通过制作不同的变量来存储不同值的麻烦。

23 56 32 54 1
0 1 2 3 4

数组的索引从0开始,根据数组的具体大小,数组的索引会递增。如果我们有一个长度为 5 的数组,那么该数组将从索引 0 开始,到索引 4 结束。因此,数组的长度定义了该数组中元素的数量。

数组在 C# 中如何工作?

在C#中,数组的长度可以是固定的,也可以是动态的。在固定长度的数组中,可以存储固定数量的项目。在动态数组中,随着新项目进入数组,大小会增加,因为数组的内存分配是动态的。在数组中,堆栈内存存储数组的变量,而托管堆存储元素。在 C# 中,数组派生自 System.数组类。如果我们有一个整型数组,那么所有元素都有各自的值,并且由于 C# 中的数组是引用类型,因此元素将保存对实际对象的引用。

如何在 C# 中创建数组?

数组的语法:

data_type [] name_of_array

1.数组声明

代码:

class Name
{
static void Main(string[]args)
{
Int32[] intarray;   //array declaration
}
}

代码说明: 在数组声明中,第一部分是数据类型,定义数组中对象的类型。第二部分是 [],它定义了数组中对象的数量,然后 next 是数组的名称,在本例中是 int array

2.数组初始化

代码:

class Name
{
static void Main(string[]args)
{
Int32[] Intarray;   //array declaration
Intarray = new Int32[4]; // array initialization
Intarray[0]= 23;  // assigning values to the elements
Intarray[1]=5;
Intarray[2]=88;
Intarray[3]=6;
}
}

代码说明:在数组初始化中,我们必须使用方括号指定数组中值的数量,并将这些值分配给每个数组元素。所以这里,intarray[0]意味着我们在第一个位置赋值,intarray[1]意味着我们在第二个位置赋值,依此类推。

3.显示数组

的值

代码:

class Name
{
static void Main(string[]args)
{
Int32[] Intarray;   //array declaration
Intarray = new Int32[4]; //array initialization
Intarray[0]= 23; //assigning values to array
Intarray[1]=5;
Intarray[2]=88;
Intarray[3]=6;
Console.WriteLine(Intarray[0]);
Console.WriteLine(Intarray[1]);
Console.WriteLine(Intarray[2]);
Console.WriteLine(Intarray[3]);
Console.ReadKey();
}
}

代码说明:Console.WriteLine 是在控制台中显示数组每个值的方法。

C# 中的数组示例

C# 中的示例和结果如下所示

示例#1

代码:

using System;
namespace ArrayDemo
{
class Name
{
static void Main(string[] args)
{
Int32[] Intarray;   // array declaration
Intarray = new Int32[4]; // array initialization
Intarray[0] = 23;   // assigning values to the array element
Intarray[1] = 5;
Intarray[2] = 88;
Intarray[3] = 6;
Console.WriteLine(Intarray[0]);
Console.WriteLine(Intarray[1]);
Console.WriteLine(Intarray[2]);
Console.WriteLine(Intarray[3]);
Console.ReadKey();
}
}
}

在上面的代码中,数组被声明并初始化为四个元素,Console.WriteLine 显示所有值。

输出:

C# 中的数组

示例#2

代码:

using System;
namespace Demo
{
class Array
{
static void Main(string[] args)
{
int[] arr = new int[4] { 10, 20, 30, 40 };
for (int i = 0; i < arr.Length; i++)    // Traverse array elements
{
Console.WriteLine(arr[i]);
}
}
}
}

在上面的代码中,数组被初始化并声明为四个元素,然后使用循环来访问数组的元素。

输出:

C# 中的数组

示例 #3

我们还可以使用foreach来访问数组的元素

代码:

using System;
namespace Demo
{
class Array
{
static void Main(string[] args)
{
int[] arr = new int[4] { 10, 20, 30, 40 };
foreach (int i in arr)
{
Console.WriteLine(i);
}
}
}
}

输出:

C# 中的数组

C# 中的数组类型

在 C# 中,我们有多种类型的数组:

  1. 一维数组。
  2. 多维数组。
  3. 锯齿状数组。

以上示例是单维数组。

多维数组

在矩形数组或多维数组中,数据以表格形式存储。

Int[,] intArray = new int[4,3]

在此,我们指定了四行三列数组的大小。

1。多维数组的声明

int[,] array = new int[3,3]; //declaration of 2D array
int[,,] array=new int[3,3,3]; //declaration of 3D array

2。多维数组的初始化

int[,] array = new int[3,3]; //declaration of 2D array
array[0,1]=10; //initialization
array[1,2]=20;
array[2,0]=30;<c/ode>

多维数组示例

代码:

using System;
namespace Demo
{
class Array
{
public static void Main()
{
int[,] intArray = new int[3, 2]{
{1, 2},
{2, 4},
{4, 8}
};
Console.WriteLine(intArray[0, 0]);
Console.WriteLine(intArray[0, 1]);
Console.WriteLine(intArray[1, 0]);
Console.WriteLine(intArray[1, 1]);
Console.WriteLine(intArray[2, 0]);
Console.WriteLine(intArray[2, 1]);
}
}
}

代码说明:上面的代码中,行和列指定为三行四列,Console.WriteLine 显示所有值。

输出:

C# 中的数组

锯齿状数组

交错数组的元素是“数组”,因为它直接存储数组。

1。锯齿状数组的声明

int[][] array = new int[3][];

第一个括号表示数组的大小,第二个括号表示数组的维度。

2。初始化并为锯齿状数组赋值

array[0] = new int[4] { 1, 2, 3, 4 };
array[1] = new int[5] { 1, 2, 3, 4,5 };

元素的大小可以不同。

以下是锯齿状数组的示例:

示例#1

代码:

using System;
namespace Demo
{
class Array
{
public static void Main()
{
int[][] array = new int[2][];// Declare the array
array[0] = new int[] { 1, 2, 6, 8 };// Initialize the array
array[1] = new int[] { 72, 51, 47, 23, 54, 13 };
// Traverse array elements
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array[i].Length; j++)
{
System.Console.Write(array[i][j] + " ");
}
System.Console.WriteLine();
}
}
}
}

输出:

C# 中的数组

示例 #2

代码:

using System;
namespace Demo
{
class Array
{
public static void Main()
{
int[][] array = new int[3][]{
new int[] { 51, 22, 43, 87 },
new int[] { 2, 3, 4, 56, 94, 23 },
new int[] { 4, 5 }
};
// Traverse array elements
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array[i].Length; j++)
{
System.Console.Write(array[i][j] + " ");
}
System.Console.WriteLine();
}
}
}
}

输出:

C# 中的数组

C# 中的数组方法

以下几点:

  1. Clear(Array, Int32, Int32): This method is used to set the element range to default based on the type of element.
  2. Clone(): This method is used to create a copy of the element.
  3. Copy(Array, Array, Int32): This method is used to copy the elements of one array to another array.
  4. Equals(Object): This method basically checks if the mentioned object is equal to the current object.
  5. Sort(Array): This method is used to sort the array.
  6. CreateInstance(Type, Int32): This method is used to create an array of a specific type, length, and size.
  7. ToString(): This is used to display string representation.
  8. GetType(): This method is used to return the type of object.
  9. IndexOf(Array, Object): This method is used to search the particular object and return the first occurrence index in a 1D array.
  10. Reverse(Array): This method is used to reverse the sequence of the elements.
  11. SetValue(Object, Int32): This method in the 1D array is used to set the value of an element.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayMethod
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[6] { 5, 8, 9, 25, 0, 7 };
// Creating an empty array
int[] arr2 = new int[6];
Console.WriteLine("length of first array: " + arr.Length); // length of array
Array.Sort(arr);  //sorting array elements
Console.Write("Sorted array elements: ");
PrintArray(arr);
Array.Copy(arr, arr2, arr.Length);  // copy elements of one array to other
Console.Write("Second array elements: ");
PrintArray(arr2);
Console.WriteLine("Get Index:\t{0}", Array.IndexOf(arr, 9));  // index of value
Array.Reverse(arr);
Console.Write("\nFirst Array elements in reverse order: ");  // reverse the elements of array
PrintArray(arr);
Array.Clear(arr, 0, 6);  //set default value of elements
PrintArray(arr);
}
static void PrintArray(int[] arr)
{
foreach (int i in arr)
{
Console.Write("\t{0}", i);
}
Console.WriteLine("\n");
}
}
}

Code Explanation: The above code shows several methods of the array in which arr. Length is used to get the length which is equal to 6, Array. Sort gives the values in sorted form. Array. Copy copies the values from the first array to the second array. Array. The reverse displays the array in reverse order, whereas Clear sets the default values of the elements.

Output:

C# 中的数组

Conclusion

So it is better to declare one array variable instead of declaring too many different variables as elements in the memory are stored sequentially, which makes it faster. Also, it’s easy to traverse, manipulate and sort the data by using arrays.

以上是C# 中的数组的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# foreach Loop下一篇:2D Arrays in C#