C# 多維數組

WBOY
發布: 2024-09-03 15:12:21
原創
590 人瀏覽過

在C#中,矩形數組或多維數組是指以矩陣格式組織元素。多維數組只能是二維或三維的。數組的維數是指變數中資料的組織格式。因此,我們可以將多維數組定義為按行或列的順序或順序排列元素的組織。

文法:

以下是多維數組的語法:

二維數組的聲明。

int[,] x=new int[1,2];
登入後複製

3D 陣列的聲明。

int[,,] x=new int[1,2,3];
登入後複製

上面的語法指定了聲明二維和三維數組 (x) 的格式。第一個陣列包含兩個元素 1 和 2,而三維陣列包含元素 1,2,3。

多維數組的初始化

多維數組可以用三種不同的方式初始化

1。完整聲明

int[,] x = new int[6,6];
登入後複製

以上規範完整地初始化了一個二維數組,包括數組類型、數組大小和new運算符的使用。

2。不使用 New 運算子進行初始化

int[,] x = { { 3,2,1 }, { 6,5,4 }, { 9,8,7 } };
登入後複製

3。初始化數組而不宣告大小

int[,] x = new int[,]{ { 3,2,1 }, { 6,5,4 }, { 9,8,7 } };
登入後複製

C# 多維數組範例

以下是 C# 中多維數組的範例:

範例#1

用來說明多維數組的宣告和初始化的程式。下面的範例說明了在 C# 中建立多維數組。

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
int[,] x = { { 3, 2, 1 }, { 6, 5, 4 }, { 9, 8, 7 } };
for (int a = 0; a < 3; a++)
{
for (int b = 0; b < 3; b++)
{
Console.Write(x[a, b] + " ");
}
Console.WriteLine();
}
}
}
}
登入後複製

輸出:

C# 多維數組

範例#2

示範二維數組的初始化、宣告以及存取元素的程式。

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
/* declaring and initialising a two dimensional array*/
int[,] b = new int[6, 2] { { 1, 2 }, { 4, 3 }, { 5, 6 }, { 8,7 }, { 9 , 10 }, { 2, 3 } };
int i, j;
/* accessing each of the elements value for the array */
for (i = 0; i < 6; i++)
{
for (j = 0; j < 2; j++)
{
Console.WriteLine("a[{0},{1}] = {2}", i, j, b[i, j]);
}
}
Console.ReadKey();
}
}
}
登入後複製

輸出:

C# 多維數組

上面的程式示範如何使用索引作為位置標記來存取多維數組中的陣列元素。

範例#3

兩個多維數組相加的程式。

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
int[,] array1 = new int[3, 3];
int[,] array2 = new int[3, 3];
int[,] resultArray = new int[3, 3];
int i, j;
Console.WriteLine("specify the members of the first array: ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
array1[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("elements of the array1: ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write("{0} ", array1[i, j]);
}
Console.Write("\n");
}
Console.WriteLine("specify the members of the array2: ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
array2[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("elements of the array2: ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write("{0} ", array2[i, j]);
}
Console.Write("\n");
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
resultArray[i, j] = array1[i, j] + array2[i, j];
}
}
Console.WriteLine("resultArray of the array1 and array2 looks as below : ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write("{0} ", resultArray[i, j]);
}
Console.Write("\n");
}
}
}
}
登入後複製

輸出:

C# 多維數組

使用上面的程序,我們完成了數組的加法操作,將第一個數組中的每個元素添加到第二個數組的計數器元素中。例如array1中第一個元素是1;同樣,ar​​ray2 中的第一個元素是 9。相加的結果應包含一個第一個元素為 10 的陣列。

優點和缺點

以下是多維數組的優點和缺點:

優點

  • 多維數組可用於組織數組中的資料子組;多維數組也可以用於在指標數組中儲存資料記憶體位址。
  • 在程式中,多維數組在開始時具有靜態大小和初始化。任何大小的擴充都需要在初始化時指定相關的大小。
  • 多維數組可以執行矩陣運算並在相同的變數分配下維護大數據值。
  • 多維數組用於實作堆疊、堆疊和佇列以及雜湊表。

缺點

  • 陣列的元素位於連續的記憶體位置;因此,元素的任何插入和刪除都會比單一元素上的類似操作更複雜。
  • 此外,元素不能插入到陣列的中間。
  • 如果靜態分配分配的內存超過所需的內存,可能會導致浪費和無法釋放未使用的內存。這可能會產生負面影響。
  • 與單維數組相比,多維數組可能會較慢,這是一個主要缺點。為了克服這個問題,我們可以用多維數組取代鋸齒狀數組。

以上是C# 多維數組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!