码迷,mamicode.com
首页 > 编程语言 > 详细

C#面向对象的方法写数组的功能

时间:2015-08-29 11:03:16      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

上一篇文章用Java方法写出了可以对数组执行的功能,然后在用实例化后的对象调用这些方法来实现这些功能;

这篇随笔改用C#语言实现同样的功能

方法类:Array

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class Array
10     {
11         /// <summary>
12         /// 遍历数组并输出
13         /// </summary>
14         /// <param name="arr"></param>
15         /// <returns></returns>
16         public int[] showArr(int[] arr) {
17             for (int i = 0; i < arr.Length;i++ )
18             {
19                 Console.Write(arr[i]+"  ");
20             }
21             return arr;
22         }
23 
24         /// <summary>
25         /// 创造一个复制数组的方法arrCopy()
26         /// </summary>
27         /// <param name="arr"></param>
28         /// <returns></returns>
29         public int[] arrCopy(int[] arr) {
30             int[] fuzhi = new int[arr.Length];
31             for(int i=0;i<arr.Length;i++){
32                 fuzhi[i] = arr[i];
33             }
34             //复制后遍历新的数组并输出
35             Console.WriteLine("复制后的数组为:");
36             for (int i = 0; i < fuzhi.Length;i++ )
37             {
38                 Console.Write(fuzhi[i]+"  ");
39             }
40             return fuzhi;
41         }
42         /// <summary>
43         /// 设置一个实现数组的反转的方法fanZhuan()
44         /// </summary>
45         /// <param name="arr"></param>
46         /// <returns></returns>
47         public int[] fanZhuan(int[] arr) {
48             for (int i = 0; i < arr.Length / 2;i++ )
49             {
50                 int temp = 0;
51                 temp = arr[i];
52                 arr[i] = arr[arr.Length - 1 - i];
53                 arr[arr.Length - 1 - i] = temp;
54             }
55             return arr;
56         }
57         /// <summary>
58         /// 冒泡排序的方法sort()
59         /// </summary>
60         /// <param name="arr"></param>
61         public void sort(int[] arr) 
62         {
63             for (int i = 0; i < arr.Length-1; i++)//总共跑arr.Length趟排好顺序
64             {
65                 for (int j = 0; j <arr.Length-1-i;j++ )//每趟比较的次数
66                 {
67                    if (arr[j] > arr[j+1]){
68                         int temp = arr[j];
69                         arr[j] = arr[j+1];
70                         arr[j+1] = temp;
71                     }
72                 }
73             }
74         }
75     }
76 }

主方法类:Program

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //创建一个数组arr
14             int[] arr = new int[] { 1, 2, 5, -6, 23, -9, 26, 100 };
15             //初始化Array类
16             Array a1 = new Array();
17             a1.arrCopy(arr);//调用复制数组的方法arrCopy()
18 
19             Console.WriteLine("\n反转后的数组为:");
20             a1.fanZhuan(arr);//调用反转方法实现数组的反转
21             a1.showArr(arr);//反转后遍历数组arr
22 
23             Console.WriteLine("\n冒泡排序后的数组为:");
24             a1.sort(arr);//调用方法对arr排序
25             a1.showArr(arr);
26 
27             Console.ReadKey();
28         }
29     }
30 }

运行结果:

技术分享

 

C#面向对象的方法写数组的功能

标签:

原文地址:http://www.cnblogs.com/yunqing/p/4768688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!