C#数组之操作方法

向大家介绍C#数组操作,可能好多人还不了解C#数组操作,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

 
  数组是相同类型的对象的集合。由于数组几乎可以为任意长度,因此可以使用数组存储数千乃至数百万个对象,但必须在创建数组时就确定其大小。数组中的每项都按索引进行访问,索引是一个数字,指示对象在数组中的存储位置或槽。数组既可用于存储引用类型,也可用于存储值类型。
 
  C#数组操作程序:
1.            using System;  
2.            using System.Collections.Generic;  
3.            using System.Text;  
4.             
5.            namespace ClassAboutArray  
6.            {  
7.            public class CreateArray  
8.            {  
9.            /// <summary> 
10.        /// 一维数组的定义 
11.        /// </summary> 
12.        public void testArr1()  
13.        {  
14.        int[] myIntArr = new int[100];  
15.        //定义一个长度为100的int数组 
16.        string[] mystringArr = new string[100];  
17.        //定义一个长度为100的string数组 
18.        object[] myObjectArr = new object[100];  
19.        //定义一个长度为100的int数组 
20.         
21.        int[] myIntArr2 = new int[] { 1, 2, 3 };   
22.        //定义一个int数组,长度为3  
23.        string[] mystringArr2 = new string[] { "油", "盐" };  
24.        //定义一个string数组,长度为2  
25.        }  
26.         
27.        /// <summary> 
28.        /// 多维数组的定义 
29.        /// </summary> 
30.        public void testArr2()  
31.        {  
32.        int[,] myIntArr = new int[10, 100];   
33.        //定义一个10*100的二维int数组 
34.        string[, ,] mystringArr = new string[2, 2, 3];   
35.        //定义一个2*2*3的三维string数组   
36.         
37.        int[,] myIntArr2 = new int[,] { { 1, 2, 3 }, { -1, -2, -3 } };  
38.        //定义一个2*3的二维int数组,并初始化 
39.        string[,] mystringArr2 = new string[,] { { "油", "盐" }, { "《围城》", "《晨露》" } };  
40.        //定义一个2*2的二维string数组,并初始化 
41.        }  
42.         
43.        /// <summary> 
44.        /// 交错数组的定义 
45.        /// </summary> 
46.        public void testArr3()  
47.        {  
48.        int[][] myJaggedArray = new int[3][];  
49.        myJaggedArray[0] = new int[5];  
50.        myJaggedArray[1] = new int[4];  
51.        myJaggedArray[2] = new int[2];  
52.         
53.        int[][] myJaggedArray2 = new int[][]  
54.         {  
55.        new int[] {1,3,5,7,9},  
56.        new int[] {0,2,4,6},  
57.        new int[] {11,22}  
58.         };  
59.        }  
60.        }  
61.         
62.        public class TraverseArray  
63.        {  
64.        /// <summary> 
65.        /// 使用GetLowerBound|GetUpperBound遍历数组 
66.        /// </summary> 
67.        public void test1()  
68.        {  
69.        //定义二维数组 
70.        string[,] myStrArr2 = new string[,] 
{ { "油", "盐" }, { "《围城》", "《晨露》" }, { "毛毛熊", "Snoopy" } };  
71.        //循环输出 
72.        for (int i = myStrArr2.GetLowerBound(0); i <= myStrArr2.GetUpperBound(0); i++)  
73.        {  
74.        Console.WriteLine("item{0}", i);  
75.        for (int j = myStrArr2.GetLowerBound(1); j <= myStrArr2.GetUpperBound(1); j++)  
76.        {  
77.        Console.WriteLine(" item{0}{1}:{2}", i, j, myStrArr2.GetValue(i, j));  
78.        }  
79.        }  
80.        }  
81.         
82.        /// <summary> 
83.        /// 使用foreach遍历数组 
84.        /// </summary> 
85.        public void test2()  
86.        {  
87.        //定义二维数组 
88.        string[,] myStrArr2 = new string[,] 
{ { "油", "盐" }, { "《围城》", "《晨露》" }, { "毛毛熊", "Snoopy" } };  
89.        //循环输出 
90.        foreach (string item in myStrArr2)  
91.        {  
92.        {  
93.        Console.WriteLine("{0}", item);  
94.        }  
95.        }  
96.        }  
97.        }  
98.         
99.        public class SortArray  
100.     {  
101.     /// <summary> 
102.     /// 利用Sort方法进行数组排序 
103.     /// </summary> 
104.     public void test1()  
105.     {  
106.     //定义数组 
107.     int[] myArr = { 5, 4, 3, 2, 1 };  
108.      
109.     //输出原始数组:原始数组:5->4->3->2->1-> 
110.     Console.WriteLine("原始数组:");  
111.     for (int i = 0; i < myArr.Length; i++)  
112.     Console.Write("{0}->", myArr[i]);  
113.     Console.WriteLine();  
114.      
115.     //对数组排序 
116.     Array.Sort(myArr);  
117.      
118.     //并输出排序后的数组:1->2->3->4->5-> 
119.     Console.WriteLine("排序以后数组:");  
120.     for (int i = 0; i < myArr.Length; i++)  
121.     Console.Write("{0}->", myArr[i]);  
122.     }  
123.      
124.     /// <summary> 
125.     /// 多个数组的关键字排序 
126.     /// </summary> 
127.     public void test2()  
128.     {  
129.     //定义数组 
130.     int[] arrSid = { 5, 4, 3, 2, 1 };  
131.     string[] arrSname = { "张三", "李四", "王五", "麻子", "淘气" };  
132.      
133.     //输出原始数组:原始数组:张三(5)->李四(4)->王五(3)->麻子(2)->淘气(1)-> 
134.     Console.WriteLine("原始数组:");  
135.     for (int i = 0; i < arrSid.Length; i++)  
136.     Console.Write("{0}({1})->", arrSname[i], arrSid[i]);  
137.     Console.WriteLine();  
138.      
139.     //根据学号关键字排序 
140.     Array.Sort(arrSid, arrSname);  
141.      
142.     //并输出排序后的数组:淘气(1)->麻子(2)->王五(3)->李四(4)->张三(5)  
143.     Console.WriteLine("排序以后数组:");  
144.     for (int i = 0; i < arrSid.Length; i++)  
145.     Console.Write("{0}({1})->", arrSname[i], arrSid[i]);  
146.     }  
147.     }  
148.      
149.     public class SearchArray  
150.     {  
151.     /// <summary> 
152.     /// 利用BinarySearch方法搜索元素 
153.     /// </summary> 
154.     public void test1()  
155.     {  
156.     //定义数组 
157.     int[] myArr = { 5, 4, 3, 2, 1 };  
158.      
159.     //对数组排序 
160.     Array.Sort(myArr);  
161.      
162.     //搜索 
163.     int target = 3;  
164.     int result = Array.BinarySearch(myArr, target); //2  
165.     Console.WriteLine("{0}的下标为{1}", target, result); //2  
166.     }  
167.      
168.     /// <summary> 
169.     /// 判断是否包含某个值 
170.     /// </summary> 
171.     public void test2()  
172.     {  
173.     //定义数组 
174.     string[] arrSname = { "张三", "李四", "王五", "麻子", "淘气" };  
175.      
176.     //判断是否含有某值 
177.     string target = "王五";  
178.     bool result = ((System.Collections.IList)arrSname).Contains(target);  
179.     Console.WriteLine("包含{0}?{1}", target, result); //true  
180.     }  
181.     }  
182.      
183.     public class ReverseArray  
184.     {  
185.     /// <summary> 
186.     /// 利用Reverse方法反转数组 
187.     /// </summary> 
188.     public void test1()  
189.     {  
190.     //定义数组 
191.     int[] myArr = { 5, 4, 3, 2, 1 };  
192.      
193.     //输出原始数组:原始数组:5->4->3->2->1-> 
194.     Console.WriteLine("原始数组:");  
195.     for (int i = 0; i < myArr.Length; i++)  
196.     Console.Write("{0}->", myArr[i]);  
197.     Console.WriteLine();  
198.      
199.     //对数组反转 
200.     Array.Reverse(myArr);  
201.      
202.     //并输出反转后的数组:1->2->3->4->5-> 
203.     Console.WriteLine("反转以后数组:");  
204.     for (int i = 0; i < myArr.Length; i++)  
205.     Console.Write("{0}->", myArr[i]);  
206.     }  
207.     }  
208.      
209.     public class CopyArray  
210.     {  
211.     /// <summary> 
212.     /// 利用Copy静态方法复制数组 
213.     /// </summary> 
214.     public void test1()  
215.     {  
216.     //定义数组 
217.     int[] myArr = { 5, 4, 3, 2, 1 };  
218.      
219.     //输出原始数组:原始数组:5->4->3->2->1-> 
220.     Console.WriteLine("原始数组:");  
221.     for (int i = 0; i < myArr.Length; i++)  
222.     Console.Write("{0}->", myArr[i]);  
223.     Console.WriteLine();  
224.      
225.     //复制数组 
226.     int[] newnewArr = new int[3];  
227.     Array.Copy(myArr, newArr, 3);  
228.      
229.     //并输出反复制的数组:5->4->3-> 
230.     Console.WriteLine("复制数组:");  
231.     for (int i = 0; i < newArr.Length; i++)  
232.     Console.Write("{0}->", newArr[i]);  
233.     }  
234.      
235.     /// <summary> 
236.     /// 利用CopyTo实例方法复制数组 
237.     /// </summary> 
238.     public void test2()  
239.     {  
240.     //定义数组 
241.     int[] myArr = { 5, 4, 3, 2, 1 };  
242.      
243.     //输出原始数组:原始数组:5->4->3->2->1-> 
244.     Console.WriteLine("原始数组:");  
245.     for (int i = 0; i < myArr.Length; i++)  
246.     Console.Write("{0}->", myArr[i]);  
247.     Console.WriteLine();  
248.      
249.     //复制数组 
250.     int[] newnewArr = new int[7];  
251.     myArr.CopyTo(newArr, 2);  
252.      
253.     //并输出反复制的数组:0->0->5->4->3->2->1-> 
254.     Console.WriteLine("复制数组:");  
255.     for (int i = 0; i < newArr.Length; i++)  
256.     Console.Write("{0}->", newArr[i]);  
257.     }  
258.     }  
259.      
260.     public class DynamicCreateArray  
261.     {  
262.     /// <summary> 
263.     /// 利用CreateInstance动态创建数组 
264.     /// </summary> 
265.     public void test1()  
266.     {  
267.     //定义长度数组 
268.     int[] lengthsArr = new int[] { 3, 4 };  
269.     int[] lowerBoundsArr = { 1, 11 };  
270.      
271.     Array arr = Array.CreateInstance(Type.GetType("System.Int32"), lengthsArr, lowerBoundsArr);  
272.      
273.     Random r = new Random(); //声明一个随机数对象 
274.     //循环赋值、输出 
275.     for (int i = arr.GetLowerBound(0) - 1; i < arr.GetUpperBound(0) - 1; i++)  
276.     {  
277.     for (int j = arr.GetLowerBound(1) - 1; j < arr.GetUpperBound(1) - 1; j++)  
278.     {  
279.     arr.SetValue((int)r.Next() % 100, i, j);//用1~100的随即数赋值 
280.     Console.WriteLine("arr[{0},{1}]={3}", i, j, arr.GetValue(i, j));  
281.     }  
282.     }  
283.     }  
284.     }  
285.     
posted on 2017-03-05 23:28  陈涛Java138  阅读(9424)  评论(0编辑  收藏  举报