class Program
{
static void Main(string[] args)
{
//-------------------一維數組------------------
// int a = 1;//聲明一個整型變量;
int[] Arr1 = new int[5];//定義一維數組長度;
int[] arr1 = new int[3] { 1, 2, 3 };//定義一維數組長度并初始化賦值;實例化;
int[] arr_1 = { 1, 2, 3 };//省略長度;直接給一維數組賦值;
//--------------------多維數組----------------
int[,] Arr2 = new int[,] { };//多維數組的定義;
int[,] Arr_2 = new int[2, 3];//多維數組的行,列;
int[,] arr_22 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] arr2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//多維數組直接賦值;
//--------------------交錯數組------------------------
int[][] Arr3 = new int[2][];//必須要定義列;否則報錯;
int[][] arr3 = new int[2][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };//定義初始化并賦值;列數可以改變;多維數組不可改變;
int[][] arr_3 = { new int[] { }, new int[] { }, new int[] { } };//定義交錯數組,注意交錯數組里的一維數組: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);//數組的維度;
Console.WriteLine(arrD.Length);//數組長度;
Console.WriteLine(arrD.LongLength);//數組元素的總和;
Console.WriteLine(arrD.IsFixedSize);//獲取一個值;表示是否有固定的值;
Console.WriteLine(arrD.IsReadOnly);//判斷數組是否可讀;
Show.show1(arrA, "前: ");
Array.Clear(arrA, 2, 3);//數組緊跟的是下標;從數組下標開始將長度個數賦值為零;參數1:數組;參數2:下標;參數3:元素個數為零;
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);//把數組E中的長度個數元素復制到數組F頭部中;參數1:被復制的數組,參數2:要復制的數組;參數3:注意是復制元素的長度個數(個數),可以不必要全部復制;
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);//把整個數組E1從指定下標復制到數組F1中去;參數1:被復制的數組;參數2:要復制的數組;參數3:操作下標;注意是從下標開始;
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);//設置數組下標的值;參數1:數組;參數2:要設置的值;參數3:設置值的下標;數組.SetValue(設值,下標);
Console.WriteLine("獲取下標的是:" + arrAA.GetValue(1));//獲取數組下標的值;參數1:數組;參數2:下標; 數組.GetValue(下標);
Show.show1(arrBB, "前: ");
Array.Reverse(arrBB);//數組逆轉 參數1:要逆轉的數組;
Show.show1(arrBB, "后: ");
//---------------------練習-----------------
//int[] arrE={33,36,66,23}; 轉變為36,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();
}
}