一維數組:
一行N列的表格
數組特點:
1.數組在創建需要指定數組的類型,數組的類型決定數組中列中存儲數據的類型。
2.數組中每個列存儲的數據都必須是相同的類型。
3.數組在創建時,一定要為其分配列的個數。
4. 數組的定義:
數組的類型[] 數組的名字=new 數組的類型[列的個數];
5.數組中的每個列都有門牌號(下標),從0開始到數組的列數-1
6.向數組存入數據
數組名[下標]=數據;
7.獲取數組中的數據
變量類型 變量=數組名[下標];
8.獲取數組中列的個數。通過數組變量的Length屬性
int 個數 =數組名.Length; //int表示變量能存儲整數。
9.利用循環將數組中的所有元素取出。
for(初始變量,循環的條件,變量的步長)
{
代碼
}
如:
int[] arr=new int[3];
arr[0]=1;
arr[1]=2;
arr[2]=3;
for(int i=0;i {
int valu=arr[i]; //循環下標獲取值
Console.WriteLine(valu);
}
數組:一旦創建,其空間大小無法改變。
如:int[] arr=new int[3]{11,22,33};//若想在arr數組中增加一個44,怎么辦?此時就必須使用下列步驟:
1.創建B數組,且A數組的元素空間等于arr.Length+1.
2.將arr數組的所有的數據導入a數組。
3.將新的數據插入到A[arr.Length]
4. arr=A;
如:
int[] arr = new int[3] { 11, 22, 33 };
int[] a = new int[arr.Length + 1];
for (int i = 0; i < arr.Length; i++)
{
a[i] = arr[i]; //遍歷arr數組,將arr數組中的數據放入a數組中。
}
a[arr.Length] = 44; //將44放入a數組中最后一個門牌號下。下標都以0開始,所以arr.Length即為最后一個下標。
arr = a; //用a數組將arr數組覆蓋掉。
foreach (int read in arr)
{
Console.WriteLine(read);
}
Sort(對數組中的數據進行排序)
public static void Sort ( Array array ):對整個一維 Array中的元素進行排序。
如:
int[] arr=new int[]{11,33,22};
Array.Sort(arr);//一旦進行排序,排序后的數組將替代原數組
foreach(int i in arr)
{
Console.WriteLine(i);//即11,22,33........排序從小到大。可對string,char等數組進行排序。
}
public static void Sort ( Array array, int index, int length ):對一維 Array 中某部分元素進行排序。array指定要排序的一維Array,index指定排序范圍的起始索引,length指定排序范圍內的元素數。
如:
int[] arr=new int[]{11,33,22,55,44};
Array.Sort(arr,1,3);//從下標為1處開始依次取3個值進行排序。
foreach(int i in arr)
{
Console.WriteLine(i);//即11,22,33,55,44........排序從小到大。可對string,char等數組進行排序。
}
中英文查詢轉換原理:
string[] ying = new string[] { "brain", "abort", "center", "get" };
string[] han = new string[] { "大腦", "放棄", "中間", "獲取" };
//如果要對ying數組排序,han數組必須排序。
Array.Sort(ying,han);//先對ying數組的排序,然后自動調節han數組中的內容。
for (int i = 0; i < ying.Length; i++)
{
Console.WriteLine(ying[i] + "=" + han[i]);
}
如何在數組中查詢指定的值?
1.BinarySearch查詢
public static int BinarySearch ( Array array, Object value )
array : 類型為int、long、short、char、byte、boolean、float、double、object的數組,注意此數組必須先進行排序。
value : 為和array 相同類型的變量,它用于指定要搜尋的數據。
若查詢失敗則返回負數。
注:若使用BinarySearch進行查詢時,切記必須對數組盡心Sort進行排序。
如:
string[] arr = new string[] { "新浪", "百度", "飛訊", "CSDN" };
Array.Sort(arr); //必須先進行排序,才能進行搜尋。
int d = Array.BinarySearch(arr, "飛訊");
Console.WriteLine(d);
2.IndexOf查詢
public static int IndexOf(Array array, Object value):搜索指定的對象,并返回整個一維 Array 中第一個匹配項的索引。array指定要搜索的一維 Array.value指定要在 array 中查找的對象,如果在整個 array 中找到 value 的匹配項,則為第一個匹配項的索引;否則返回-1.
public static int IndexOf ( Array array, Object value, int startIndex ):搜索指定的對象,并返回一維 Array 中從指定索引到最后一個元素這部分元素中第一個匹配項的索引, startIndex 指定搜索的起始索引。如果在 array 中從 startIndex 到最后一個元素這部分元素中第一個與 value 匹配的項的索引;否則返回-1。
public static int IndexOf ( Array array, Object value, int startIndex, int count ) :搜索指定的對象,并返回一維 Array 中從指定索引開始包含指定個元素的這部分元素中第一個匹配項的索引,count 指定要搜索的部分中的元素數。
如:
string[] arr = new string[] { "新浪", "百度", "飛訊", "CSDN" };
//可以不進行排序
int d = Array.BinarySearch(arr, "飛訊");
Console.WriteLine(d);
二維數組:一個N行N列的表格。
聲明:
類型[,] 數組名=new 類型[行的元素個數,列的元素個數];
int[,] a=new int[2,3]; 二行三列
賦予數據:
數組名[行,列]=數據
獲取數據:
類型 變量名=數組名[行,列];
如:
string[,] arr=new string[2,3];
arr[0,0] = "a";
arr[0,1] = "b";
arr[0,2] = "c";
arr[1,0] = "d";
arr[1,1] = "e";
arr[1,2] = "f";
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine(arr[i, j]);
}
}
********************************************
int[,] a = new int[2, 3] { {11,22,33 }, {44,55,66 } }; //括號的添加從外到內,行列的維數從左到右看
int[,] b = new int[3, 4] { {11,22,33,44 }, {55,66,77,88 }, {99,999,999,999 } };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
Console.WriteLine(b[i, j]);
}
}
GetLength(獲取數組某一維的元素個數)
public int GetLength ( int dimension ):dimension用于指定要獲取數組維數,維數是從零開始表示,返回值指定維中的元素個數。
如:
string[,] arr=new string[2,3];
int num=arr.GetLength(0);//獲取數組中第一維的元素個數
Console.WriteLine(num);//即2.
若dimension超過維數索引將會報錯。
所以在執行某些不確定的程序代碼時,可用try..catch(異常捕獲)包括起來。
try
{
//如果在此范圍內的代碼有異常,程序就跳轉到catch中,Exception表示接收所有的錯誤信息的對象。
string[,] arr=new string[2,3];
int num=arr.GetLength(2);//超出索引。報錯。執行catch。
Console.WriteLine(num);//報錯。不會執行。
}
catch(Exception e)
{
Console.WriteLine(e);
}
二維數組之鋸齒數組(不規則數組)
聲明:
類型[][] 數組名=new 類型[行的個數][];
數組名[0]=new 類型[內嵌一維數組的元素個數];
如:
int[][] arr = new int[2][];
a[0] = new int[3] { 11, 22, 33 };
a[1] = new int[2] { 44, 55 };
foreach (int[] fs in arr)
{
foreach (int obj in fs)
{
Console.WriteLine(obj);
}
}
三維數組
聲明:
類型[][][] a=new 類型[行的元素個數][][];
a[0]=new 類型[列的個數][];
如:
int[][][] a = new int[2][][];
a[0] = new int[3][];
a[1] = new int[2][];
a[0][0] = new int[] { 11, 22 };
a[0][1] = new int[] { 33, 44 };
a[0][2] = new int[] { 5, 6 };
a[1][0] = new int[] { 77, 88 };
a[1][1] = new int[] { 99 };
Console.WriteLine("第一行第二個列的第一個高的數:"+a[0][1][0]);//第一行第二個列的第一個高的數
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < a[i].Length; j++)
{
for (int u = 0; u < a[i][j].Length; u++)
{
Console.WriteLine(a[i][j][u]);
}
}
}
最近在一個新站點看到的,覺得比較實用,貼上來分享分享。
原文http://www.ffxun.com/content.aspx?id=111&domain=2