首頁  >  文章  >  後端開發  >  C# 中的數組

C# 中的數組

王林
王林原創
2024-09-03 15:11:33376瀏覽

陣列是一組物件。數組中存在的元素具有相同的資料類型。可能是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 循環下一篇:C# foreach 循環