C#數(shù)組操作詳細(xì)剖析
本文向大家介紹C#數(shù)組操作,可能好多人還不了解C#數(shù)組操作,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。
數(shù)組是相同類型的對(duì)象的集合。由于數(shù)組幾乎可以為任意長(zhǎng)度,因此可以使用數(shù)組存儲(chǔ)數(shù)千乃至數(shù)百萬個(gè)對(duì)象,但必須在創(chuàng)建數(shù)組時(shí)就確定其大小。數(shù)組中的每項(xiàng)都按索引進(jìn)行訪問,索引是一個(gè)數(shù)字,指示對(duì)象在數(shù)組中的存儲(chǔ)位置或槽。數(shù)組既可用于存儲(chǔ) 引用類型,也可用于存儲(chǔ) 值類型。
C#數(shù)組操作程序:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace ClassAboutArray
- {
- public class CreateArray
- {
- /// <summary>
- /// 一維數(shù)組的定義
- /// </summary>
- public void testArr1()
- {
- int[] myIntArr = new int[100];
- //定義一個(gè)長(zhǎng)度為100的int數(shù)組
- string[] mystringArr = new string[100];
- //定義一個(gè)長(zhǎng)度為100的string數(shù)組
- object[] myObjectArr = new object[100];
- //定義一個(gè)長(zhǎng)度為100的int數(shù)組
- int[] myIntArr2 = new int[] { 1, 2, 3 };
- //定義一個(gè)int數(shù)組,長(zhǎng)度為3
- string[] mystringArr2 = new string[] { "油", "鹽" };
- //定義一個(gè)string數(shù)組,長(zhǎng)度為2
- }
- /// <summary>
- /// 多維數(shù)組的定義
- /// </summary>
- public void testArr2()
- {
- int[,] myIntArr = new int[10, 100];
- //定義一個(gè)10*100的二維int數(shù)組
- string[, ,] mystringArr = new string[2, 2, 3];
- //定義一個(gè)2*2*3的三維string數(shù)組
- int[,] myIntArr2 = new int[,] { { 1, 2, 3 }, { -1, -2, -3 } };
- //定義一個(gè)2*3的二維int數(shù)組,并初始化
- string[,] mystringArr2 = new string[,] { { "油", "鹽" }, { "《圍城》", "《晨露》" } };
- //定義一個(gè)2*2的二維string數(shù)組,并初始化
- }
- /// <summary>
- /// 交錯(cuò)數(shù)組的定義
- /// </summary>
- public void testArr3()
- {
- int[][] myJaggedArray = new int[3][];
- myJaggedArray[0] = new int[5];
- myJaggedArray[1] = new int[4];
- myJaggedArray[2] = new int[2];
- int[][] myJaggedArray2 = new int[][]
- {
- new int[] {1,3,5,7,9},
- new int[] {0,2,4,6},
- new int[] {11,22}
- };
- }
- }
- public class TraverseArray
- {
- /// <summary>
- /// 使用GetLowerBound|GetUpperBound遍歷數(shù)組
- /// </summary>
- public void test1()
- {
- //定義二維數(shù)組
- string[,] myStrArr2 = new string[,]
{ { "油", "鹽" }, { "《圍城》", "《晨露》" }, { "毛毛熊", "Snoopy" } };- //循環(huán)輸出
- for (int i = myStrArr2.GetLowerBound(0); i <= myStrArr2.GetUpperBound(0); i++)
- {
- Console.WriteLine("item{0}", i);
- for (int j = myStrArr2.GetLowerBound(1); j <= myStrArr2.GetUpperBound(1); j++)
- {
- Console.WriteLine(" item{0}{1}:{2}", i, j, myStrArr2.GetValue(i, j));
- }
- }
- }
- /// <summary>
- /// 使用foreach遍歷數(shù)組
- /// </summary>
- public void test2()
- {
- //定義二維數(shù)組
- string[,] myStrArr2 = new string[,]
{ { "油", "鹽" }, { "《圍城》", "《晨露》" }, { "毛毛熊", "Snoopy" } };- //循環(huán)輸出
- foreach (string item in myStrArr2)
- {
- {
- Console.WriteLine("{0}", item);
- }
- }
- }
- }
- public class SortArray
- {
- /// <summary>
- /// 利用Sort方法進(jìn)行數(shù)組排序
- /// </summary>
- public void test1()
- {
- //定義數(shù)組
- int[] myArr = { 5, 4, 3, 2, 1 };
- //輸出原始數(shù)組:原始數(shù)組:5->4->3->2->1->
- Console.WriteLine("原始數(shù)組:");
- for (int i = 0; i < myArr.Length; i++)
- Console.Write("{0}->", myArr[i]);
- Console.WriteLine();
- //對(duì)數(shù)組排序
- Array.Sort(myArr);
- //并輸出排序后的數(shù)組:1->2->3->4->5->
- Console.WriteLine("排序以后數(shù)組:");
- for (int i = 0; i < myArr.Length; i++)
- Console.Write("{0}->", myArr[i]);
- }
- /// <summary>
- /// 多個(gè)數(shù)組的關(guān)鍵字排序
- /// </summary>
- public void test2()
- {
- //定義數(shù)組
- int[] arrSid = { 5, 4, 3, 2, 1 };
- string[] arrSname = { "張三", "李四", "王五", "麻子", "淘氣" };
- //輸出原始數(shù)組:原始數(shù)組:張三(5)->李四(4)->王五(3)->麻子(2)->淘氣(1)->
- Console.WriteLine("原始數(shù)組:");
- for (int i = 0; i < arrSid.Length; i++)
- Console.Write("{0}({1})->", arrSname[i], arrSid[i]);
- Console.WriteLine();
- //根據(jù)學(xué)號(hào)關(guān)鍵字排序
- Array.Sort(arrSid, arrSname);
- //并輸出排序后的數(shù)組:淘氣(1)->麻子(2)->王五(3)->李四(4)->張三(5)
- Console.WriteLine("排序以后數(shù)組:");
- for (int i = 0; i < arrSid.Length; i++)
- Console.Write("{0}({1})->", arrSname[i], arrSid[i]);
- }
- }
- public class SearchArray
- {
- /// <summary>
- /// 利用BinarySearch方法搜索元素
- /// </summary>
- public void test1()
- {
- //定義數(shù)組
- int[] myArr = { 5, 4, 3, 2, 1 };
- //對(duì)數(shù)組排序
- Array.Sort(myArr);
- //搜索
- int target = 3;
- int result = Array.BinarySearch(myArr, target); //2
- Console.WriteLine("{0}的下標(biāo)為{1}", target, result); //2
- }
- /// <summary>
- /// 判斷是否包含某個(gè)值
- /// </summary>
- public void test2()
- {
- //定義數(shù)組
- string[] arrSname = { "張三", "李四", "王五", "麻子", "淘氣" };
- //判斷是否含有某值
- string target = "王五";
- bool result = ((System.Collections.IList)arrSname).Contains(target);
- Console.WriteLine("包含{0}?{1}", target, result); //true
- }
- }
- public class ReverseArray
- {
- /// <summary>
- /// 利用Reverse方法反轉(zhuǎn)數(shù)組
- /// </summary>
- public void test1()
- {
- //定義數(shù)組
- int[] myArr = { 5, 4, 3, 2, 1 };
- //輸出原始數(shù)組:原始數(shù)組:5->4->3->2->1->
- Console.WriteLine("原始數(shù)組:");
- for (int i = 0; i < myArr.Length; i++)
- Console.Write("{0}->", myArr[i]);
- Console.WriteLine();
- //對(duì)數(shù)組反轉(zhuǎn)
- Array.Reverse(myArr);
- //并輸出反轉(zhuǎn)后的數(shù)組:1->2->3->4->5->
- Console.WriteLine("反轉(zhuǎn)以后數(shù)組:");
- for (int i = 0; i < myArr.Length; i++)
- Console.Write("{0}->", myArr[i]);
- }
- }
- public class CopyArray
- {
- /// <summary>
- /// 利用Copy靜態(tài)方法復(fù)制數(shù)組
- /// </summary>
- public void test1()
- {
- //定義數(shù)組
- int[] myArr = { 5, 4, 3, 2, 1 };
- //輸出原始數(shù)組:原始數(shù)組:5->4->3->2->1->
- Console.WriteLine("原始數(shù)組:");
- for (int i = 0; i < myArr.Length; i++)
- Console.Write("{0}->", myArr[i]);
- Console.WriteLine();
- //復(fù)制數(shù)組
- int[] newnewArr = new int[3];
- Array.Copy(myArr, newArr, 3);
- //并輸出反復(fù)制的數(shù)組:5->4->3->
- Console.WriteLine("復(fù)制數(shù)組:");
- for (int i = 0; i < newArr.Length; i++)
- Console.Write("{0}->", newArr[i]);
- }
- /// <summary>
- /// 利用CopyTo實(shí)例方法復(fù)制數(shù)組
- /// </summary>
- public void test2()
- {
- //定義數(shù)組
- int[] myArr = { 5, 4, 3, 2, 1 };
- //輸出原始數(shù)組:原始數(shù)組:5->4->3->2->1->
- Console.WriteLine("原始數(shù)組:");
- for (int i = 0; i < myArr.Length; i++)
- Console.Write("{0}->", myArr[i]);
- Console.WriteLine();
- //復(fù)制數(shù)組
- int[] newnewArr = new int[7];
- myArr.CopyTo(newArr, 2);
- //并輸出反復(fù)制的數(shù)組:0->0->5->4->3->2->1->
- Console.WriteLine("復(fù)制數(shù)組:");
- for (int i = 0; i < newArr.Length; i++)
- Console.Write("{0}->", newArr[i]);
- }
- }
- public class DynamicCreateArray
- {
- /// <summary>
- /// 利用CreateInstance動(dòng)態(tài)創(chuàng)建數(shù)組
- /// </summary>
- public void test1()
- {
- //定義長(zhǎng)度數(shù)組
- int[] lengthsArr = new int[] { 3, 4 };
- int[] lowerBoundsArr = { 1, 11 };
- Array arr = Array.CreateInstance(Type.GetType("System.Int32"), lengthsArr, lowerBoundsArr);
- Random r = new Random(); //聲明一個(gè)隨機(jī)數(shù)對(duì)象
- //循環(huán)賦值、輸出
- for (int i = arr.GetLowerBound(0) - 1; i < arr.GetUpperBound(0) - 1; i++)
- {
- for (int j = arr.GetLowerBound(1) - 1; j < arr.GetUpperBound(1) - 1; j++)
- {
- arr.SetValue((int)r.Next() % 100, i, j);//用1~100的隨即數(shù)賦值
- Console.WriteLine("arr[{0},{1}]={3}", i, j, arr.GetValue(i, j));
- }
- }
- }
- }
- }
【編輯推薦】