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ù)組初始化,需要的朋友可以參考下
題外話(huà):學(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)始
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)容
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ù)組
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)型。
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;
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
//定義交錯(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();
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。
總是有各種驚喜,震驚!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)文章
windows中使用C# 調(diào)用 C語(yǔ)言生成的dll
本文給大家介紹的是在Windows系統(tǒng)中使用C#調(diào)用C語(yǔ)言生成的DLL文件的一種思路,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2016-11-11C#實(shí)現(xiàn)無(wú)限級(jí)聯(lián)下拉列表框
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)無(wú)限級(jí)聯(lián)下拉列表框的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-03-03C#使用TcpListener及TcpClient開(kāi)發(fā)一個(gè)簡(jiǎn)單的Chat工具實(shí)例
下面小編就為大家分享一篇C#使用TcpListener及TcpClient開(kāi)發(fā)一個(gè)簡(jiǎn)單的Chat工具實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12C#實(shí)現(xiàn)批量Word轉(zhuǎn)換Html的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#批量Word轉(zhuǎn)換Html的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-123種方法實(shí)現(xiàn)WindowsForm切換窗口
這篇文章主要介紹了3種方法實(shí)現(xiàn)WindowsForm切換窗口,文中講解非常詳細(xì),示例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07c#循環(huán)中產(chǎn)生偽隨機(jī)數(shù)
在循環(huán)中產(chǎn)生多個(gè)隨機(jī)數(shù),容易出現(xiàn)連續(xù)相同的數(shù)據(jù),最終的多個(gè)隨機(jī)數(shù)并不隨機(jī),而是帶有某種規(guī)律性。2010-06-06