欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#數(shù)組初始化簡(jiǎn)析

 更新時(shí)間:2012年11月21日 11:17:50   作者:  
C#數(shù)組與其它C系列語(yǔ)言有著很多的不同,以前接觸的時(shí)候理解出現(xiàn)很大的偏差。尤其是對(duì)多維數(shù)組的認(rèn)識(shí),本文將詳細(xì)介紹C#數(shù)組初始化,需要的朋友可以參考下
題外話:學(xué)習(xí).NET已經(jīng)有一年了,從C#->ASP.NET->WPF。主要以看電子書(shū)為主,比較少寫(xiě)代碼?,F(xiàn)在回頭學(xué)習(xí)以前接觸過(guò)的,隨著知識(shí)與經(jīng)驗(yàn)的的積累。

總是有各種驚喜,震驚!C#數(shù)組就是其中之一,我把它作為自己博客園的處女作。
C#數(shù)組與其它C系列語(yǔ)言有著很多的不同,以前接觸的時(shí)候理解出現(xiàn)很大的偏差。尤其是對(duì)多維數(shù)組的認(rèn)識(shí)。多維數(shù)組與C語(yǔ)言相比是一個(gè)新概念。而最開(kāi)始的
時(shí)候我把它當(dāng)成交錯(cuò)數(shù)組的特殊類(lèi)型。

首先重二維數(shù)組與簡(jiǎn)單的交錯(cuò)數(shù)組的初始化與訪問(wèn)開(kāi)始
復(fù)制代碼 代碼如下:

int[,] nums={
{1,2,3},
{1,2,0}
};
for (int i = nums.GetLowerBound(0); i <= nums.GetUpperBound(0); i++)
{
for (int j = nums.GetLowerBound(1); j <= nums.GetUpperBound(1); j++)
{
Console.WriteLine(nums[i,j]);
Console.WriteLine(nums.GetValue(i,j));
}
}
foreach (var num in nums)
{
Console.WriteLine(num);
}
//對(duì)任意維度的數(shù)組,都可以這樣快速訪問(wèn),只是foreach不能修改變量。

而交錯(cuò)數(shù)組也能實(shí)現(xiàn)差不多的內(nèi)容
復(fù)制代碼 代碼如下:

int[][] nums2 ={
new int[]{1,2,3},
new int[]{1,2,0}
};
for (int i = nums2.GetLowerBound(0); i <= nums2.GetUpperBound(0); i++)
{
for (int j = nums2[i].GetLowerBound(0); j <= nums2[i].GetUpperBound(0); j++)
{
Console.WriteLine(nums2[i][j]);
}
}
foreach (var ia in nums2)
{
foreach (var i in ia)
{
Console.WriteLine(i);
}
}

多維數(shù)組存儲(chǔ)的數(shù)據(jù)可以用交錯(cuò)數(shù)組替代。交錯(cuò)數(shù)組是一個(gè)有高維度的特殊數(shù)組。而交錯(cuò)數(shù)組是數(shù)組的數(shù)組。而且數(shù)組有一個(gè)很重要的性質(zhì),
數(shù)組里面儲(chǔ)蓄的必須是相同的類(lèi)型!這對(duì)理解各種復(fù)雜數(shù)組是很重要的。
復(fù)雜的交錯(cuò)數(shù)組
復(fù)制代碼 代碼如下:

bool[][][] cells31 = new bool[2][][]
{
new bool[2][]
{
new bool[] {false},
new bool[] {true}
},
new bool[3][]
{
new bool[] {false},
new bool[] {true},
new bool[] {true}
}
};

我們必須這樣初始化 有一大堆new 因?yàn)榻诲e(cuò)數(shù)組是數(shù)組的數(shù)組,所以我們以前一直嵌套。但是需要很多的數(shù)組類(lèi)型,也可以創(chuàng)建無(wú)數(shù)的數(shù)組類(lèi)型。
復(fù)制代碼 代碼如下:

Console.WriteLine("交錯(cuò)數(shù)組類(lèi)型");
Console.WriteLine(cells31[0].GetType());
Console.WriteLine(cells31[0][0].GetType());
Console.WriteLine(cells31[0][0][0].GetType());
//交錯(cuò)數(shù)組類(lèi)型
//System.Boolean[][]
//System.Boolean[]
//System.Boolean
//這是交錯(cuò)數(shù)組里面的類(lèi)型。
// bool[2][] 與boo[3][] 是相同的類(lèi)型,所以我們創(chuàng)建存儲(chǔ)結(jié)構(gòu)不一致的數(shù)組

接下來(lái)是最復(fù)雜的類(lèi)型。將交錯(cuò)數(shù)組與多維數(shù)組混合起來(lái)。如果能初始化下面的數(shù)組那么應(yīng)該就理解的比較透徹了吧!
bool [][,,][][,,][]Foo;
我選擇一個(gè)簡(jiǎn)單點(diǎn)作為示例 bool [][,][]Foo;
復(fù)制代碼 代碼如下:

bool[][,][] Foo = new bool[1][,][]
{
new bool[2,2][]
{
{
new bool[2] {false, true},
new bool[2] {false, true}
},
{
new bool[2] {false, true},
new bool[2] {false, true}
}
}
};
Console.WriteLine("混合數(shù)組類(lèi)型");
Console.WriteLine(Foo.GetType());
Console.WriteLine(Foo[0].GetType());
Console.WriteLine(Foo[0][0,0].GetType());
Console.WriteLine(Foo[0][0, 0][0].GetType());
//結(jié)果 混合數(shù)組類(lèi)型
//system.boolean[][,][]
//system.boolean[][,]
//system.boolean[]
//system.boolean

復(fù)制代碼 代碼如下:

//定義交錯(cuò)數(shù)組:一維數(shù)組存放(二維int數(shù)組存放(一維int數(shù)組存放(四維int數(shù)組)))
//標(biāo)準(zhǔn)的C#定義描述 array of( multi-array of( array of (nulti-array)))
int[][,][][, , ,] arr = new int[10][,][][,,,];
//初始化 二維int數(shù)組存放(一維int數(shù)組存放(四維int數(shù)組))
arr[4] = new int[1, 2][][,,,];
//初始化 一維int數(shù)組存放(四維int數(shù)組)
arr[4][0, 1] = new int[3][, , ,];
//初始化 四維int數(shù)組
arr[4][0, 1][2] = new int[1, 2, 3, 4];
Console.WriteLine(arr.GetType());
Console.WriteLine(arr[4].GetType());
Console.WriteLine(arr[4][0, 1].GetType());
Console.WriteLine(arr[4][0, 1][2].GetType());
//System.Int32[,,,][][,][]
//System.Int32[,,,][][,]
//System.Int32[,,,][]
//System.Int32[,,,]
//C#編譯器生成的名字與我們聲明的是倒著的。理解起來(lái)應(yīng)該也沒(méi)差異吧

現(xiàn)在應(yīng)該比較清晰了吧。我也不知道到底是不是每個(gè)程序員都理解這些,不過(guò)我是花了不少時(shí)間才明白的。
最后再考慮一下對(duì)數(shù)組方法的影響。尤其是 Clear();
復(fù)制代碼 代碼如下:

Console.WriteLine(Foo[0][0,0][0]);
//輸出為Flase
Array.Clear(Foo,0,1);
Console.WriteLine(Foo[0][0, 0][0]);
//這里會(huì)引發(fā)空引用異常。因?yàn)?bool[][,]的類(lèi)型的值已經(jīng)變?yōu)閚ull。

相關(guān)文章

最新評(píng)論