class Program
{
static void Main(string[] args)
{
//-------------------一維數(shù)組------------------
// int a = 1;//聲明一個整型變量肿男;
int[] Arr1 = new int[5];//定義一維數(shù)組長度注簿;
int[] arr1 = new int[3] { 1, 2, 3 };//定義一維數(shù)組長度并初始化賦值扳抽;實例化;
int[] arr_1 = { 1, 2, 3 };//省略長度耗拓;直接給一維數(shù)組賦值;
//--------------------多維數(shù)組----------------
int[,] Arr2 = new int[,] { };//多維數(shù)組的定義奏司;
int[,] Arr_2 = new int[2, 3];//多維數(shù)組的行乔询,列;
int[,] arr_22 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] arr2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//多維數(shù)組直接賦值结澄;
//--------------------交錯數(shù)組------------------------
int[][] Arr3 = new int[2][];//必須要定義列;否則報錯岸夯;
int[][] arr3 = new int[2][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };//定義初始化并賦值麻献;列數(shù)可以改變;多維數(shù)組不可改變猜扮;
int[][] arr_3 = { new int[] { }, new int[] { }, new int[] { } };//定義交錯數(shù)組勉吻,注意交錯數(shù)組里的一維數(shù)組:new int[]{};
//------------Array對象使用方法----------------
int[] arrA = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] arrB = { 18, 28, 38, 48, 58, 68, 78, 88, 98 };
int[,] arrC = { { 1, 2, 3 }, { 4, 5, 6 } };
int[][] arrD = { new int[] { 1, 2, 3, 4 }, new int[] { 6, 7, 8 }, new int[] { 9, 0 } };
Console.WriteLine(arrD.Rank);//數(shù)組的維度;
Console.WriteLine(arrD.Length);//數(shù)組長度旅赢;
Console.WriteLine(arrD.LongLength);//數(shù)組元素的總和齿桃;
Console.WriteLine(arrD.IsFixedSize);//獲取一個值;表示是否有固定的值煮盼;
Console.WriteLine(arrD.IsReadOnly);//判斷數(shù)組是否可讀短纵;
Show.show1(arrA, "前: ");
Array.Clear(arrA, 2, 3);//數(shù)組緊跟的是下標(biāo);從數(shù)組下標(biāo)開始將長度個數(shù)賦值為零僵控;參數(shù)1:數(shù)組香到;參數(shù)2:下標(biāo);參數(shù)3:元素個數(shù)為零;
Show.show1(arrA, "后: ");
bool[] arrTF = { true, true, true, true, false, false, false };
Show.showBool(arrTF, "前: ");
Array.Clear(arrTF, 1, 2);//布爾類型
Show.showBool(arrTF, "后: ");
string[] arrStr = { "Hello", " ", "world", "!" };
Show.showStr(arrStr, "前");
Array.Clear(arrStr, 1, 2);//string 類型
Show.showStr(arrStr, "后");
int[] arrE = { 1, 1, 1 };
int[] arrF = { 8, 8, 8, 8, 8, 8, 8, 8, 8 };
Show.show1(arrF, "前: ");
Array.Copy(arrE, arrF, arrE.Length);//把數(shù)組E中的長度個數(shù)元素復(fù)制到數(shù)組F頭部中悠就;參數(shù)1:被復(fù)制的數(shù)組千绪,參數(shù)2:要復(fù)制的數(shù)組;參數(shù)3:注意是復(fù)制元素的長度個數(shù)(個數(shù))梗脾,可以不必要全部復(fù)制荸型;
Show.show1(arrF, "后: ");
int[] arrE1 = { 1, 1, 1 };
int[] arrF1 = { 8, 8, 8, 8, 8, 8, 8, 8, 8 };
Show.show1(arrF1, "前: ");
arrE1.CopyTo(arrF1, arrF1.Length - arrE1.Length);//把整個數(shù)組E1從指定下標(biāo)復(fù)制到數(shù)組F1中去;參數(shù)1:被復(fù)制的數(shù)組炸茧;參數(shù)2:要復(fù)制的數(shù)組瑞妇;參數(shù)3:操作下標(biāo);注意是從下標(biāo)開始宇立;
Show.show1(arrF1, "后: ");
int[] arrAA = { 1, 2, 3, 4, 5, 6, 7, 8 };
int[] arrBB = { 21, 22, 23, 24, 25, 26, 27, 28 };
arrAA.SetValue(88, 1);//設(shè)置數(shù)組下標(biāo)的值踪宠;參數(shù)1:數(shù)組;參數(shù)2:要設(shè)置的值妈嘹;參數(shù)3:設(shè)置值的下標(biāo)柳琢;數(shù)組.SetValue(設(shè)值,下標(biāo));
Console.WriteLine("獲取下標(biāo)的是:" + arrAA.GetValue(1));//獲取數(shù)組下標(biāo)的值润脸;參數(shù)1:數(shù)組柬脸;參數(shù)2:下標(biāo); 數(shù)組.GetValue(下標(biāo));
Show.show1(arrBB, "前: ");
Array.Reverse(arrBB);//數(shù)組逆轉(zhuǎn) 參數(shù)1:要逆轉(zhuǎn)的數(shù)組毙驯;
Show.show1(arrBB, "后: ");
//---------------------練習(xí)-----------------
//int[] arrE={33,36,66,23}; 轉(zhuǎn)變?yōu)?6,66,33,23
int[] arrEE = { 33, 36, 66, 23 };
int[] arrEE1 = new int[arrEE.Length];
Show.show1(arrEE, "前: ");
arrEE.CopyTo(arrEE1, 0);
Array.Copy(arrEE, 1, arrEE1, 0, 2);
Array.Copy(arrEE, 0, arrEE1, 2, 1);
Show.show1(arrEE1, "后: ");
Console.ReadKey();
}
}
class Show
{
public static void show1(int[] arr, string str)
{
Console.Write(str);
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
public static void showBool(bool[] arr, string str)
{
Console.Write(str);
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
public static void showStr(string[] arr, string str)
{
Console.Write(str);
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
}