C#數(shù)組初始化簡(jiǎn)析
更新時(shí)間:2012年11月21日 11:17:50 作者:
C#數(shù)組與其它C系列語言有著很多的不同,以前接觸的時(shí)候理解出現(xiàn)很大的偏差。尤其是對(duì)多維數(shù)組的認(rèn)識(shí),本文將詳細(xì)介紹C#數(shù)組初始化,需要的朋友可以參考下
題外話:學(xué)習(xí).NET已經(jīng)有一年了,從C#->ASP.NET->WPF。主要以看電子書為主,比較少寫代碼。現(xiàn)在回頭學(xué)習(xí)以前接觸過的,隨著知識(shí)與經(jīng)驗(yàn)的的積累。
總是有各種驚喜,震驚!C#數(shù)組就是其中之一,我把它作為自己博客園的處女作。
C#數(shù)組與其它C系列語言有著很多的不同,以前接觸的時(shí)候理解出現(xiàn)很大的偏差。尤其是對(duì)多維數(shù)組的認(rèn)識(shí)。多維數(shù)組與C語言相比是一個(gè)新概念。而最開始的
時(shí)候我把它當(dāng)成交錯(cuò)數(shù)組的特殊類型。
首先重二維數(shù)組與簡(jiǎn)單的交錯(cuò)數(shù)組的初始化與訪問開始
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ù)組,都可以這樣快速訪問,只是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ǔ)蓄的必須是相同的類型!這對(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ù)組類型,也可以創(chuàng)建無數(shù)的數(shù)組類型。
Console.WriteLine("交錯(cuò)數(shù)組類型");
Console.WriteLine(cells31[0].GetType());
Console.WriteLine(cells31[0][0].GetType());
Console.WriteLine(cells31[0][0][0].GetType());
//交錯(cuò)數(shù)組類型
//System.Boolean[][]
//System.Boolean[]
//System.Boolean
//這是交錯(cuò)數(shù)組里面的類型。
// bool[2][] 與boo[3][] 是相同的類型,所以我們創(chuàng)建存儲(chǔ)結(jié)構(gòu)不一致的數(shù)組
接下來是最復(fù)雜的類型。將交錯(cuò)數(shù)組與多維數(shù)組混合起來。如果能初始化下面的數(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ù)組類型");
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ù)組類型
//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#編譯器生成的名字與我們聲明的是倒著的。理解起來應(yīng)該也沒差異吧
現(xiàn)在應(yīng)該比較清晰了吧。我也不知道到底是不是每個(gè)程序員都理解這些,不過我是花了不少時(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[][,]的類型的值已經(jīng)變?yōu)閚ull。
總是有各種驚喜,震驚!C#數(shù)組就是其中之一,我把它作為自己博客園的處女作。
C#數(shù)組與其它C系列語言有著很多的不同,以前接觸的時(shí)候理解出現(xiàn)很大的偏差。尤其是對(duì)多維數(shù)組的認(rèn)識(shí)。多維數(shù)組與C語言相比是一個(gè)新概念。而最開始的
時(shí)候我把它當(dāng)成交錯(cuò)數(shù)組的特殊類型。
首先重二維數(shù)組與簡(jiǎn)單的交錯(cuò)數(shù)組的初始化與訪問開始
復(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ù)組,都可以這樣快速訪問,只是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ǔ)蓄的必須是相同的類型!這對(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ù)組類型,也可以創(chuàng)建無數(shù)的數(shù)組類型。
復(fù)制代碼 代碼如下:
Console.WriteLine("交錯(cuò)數(shù)組類型");
Console.WriteLine(cells31[0].GetType());
Console.WriteLine(cells31[0][0].GetType());
Console.WriteLine(cells31[0][0][0].GetType());
//交錯(cuò)數(shù)組類型
//System.Boolean[][]
//System.Boolean[]
//System.Boolean
//這是交錯(cuò)數(shù)組里面的類型。
// bool[2][] 與boo[3][] 是相同的類型,所以我們創(chuàng)建存儲(chǔ)結(jié)構(gòu)不一致的數(shù)組
接下來是最復(fù)雜的類型。將交錯(cuò)數(shù)組與多維數(shù)組混合起來。如果能初始化下面的數(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ù)組類型");
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ù)組類型
//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#編譯器生成的名字與我們聲明的是倒著的。理解起來應(yīng)該也沒差異吧
現(xiàn)在應(yīng)該比較清晰了吧。我也不知道到底是不是每個(gè)程序員都理解這些,不過我是花了不少時(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[][,]的類型的值已經(jīng)變?yōu)閚ull。
相關(guān)文章
Unity實(shí)現(xiàn)OCR文字識(shí)別功能
這篇文章主要介紹了通過Unity接入百度AI接口,實(shí)現(xiàn)OCR文字識(shí)別功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的參考價(jià)值,需要的可以了解一下2022-01-01
c#基數(shù)排序Radix sort的實(shí)現(xiàn)方法
這篇文章主要介紹了c#基數(shù)排序Radix sort的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01
C#向PPT文檔插入圖片以及導(dǎo)出圖片的實(shí)例
PowerPoint演示文稿是我們?nèi)粘9ぷ髦谐S玫霓k公軟件之一,本篇文章介紹了C#向PPT文檔插入圖片以及導(dǎo)出圖片的實(shí)例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12
webBrowser執(zhí)行js的方法,并返回值,c#后臺(tái)取值的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄獁ebBrowser執(zhí)行js的方法,并返回值,c#后臺(tái)取值的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
比Math類庫abs()方法性能更高的取絕對(duì)值方法介紹
這篇文章主要給大家介紹了一種比Math類庫abs()方法性能更高的取絕對(duì)值方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

