zoukankan      html  css  js  c++  java
  • C#阵列Array排序

    五一假期回来,练习一下C#的一些知识,了解一下排序。

    练习数据:

    int[] ints = { 16, 75, 1, 39, 22, 43, 3, 68, 55 };


    写一个类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplicationDemo
    {
        public class Bw
        {
            public int[] ArrayData { get; set; }
    
            public Bw() { }
    
            public Bw(int[] myArrayData)
            {
                this.ArrayData = myArrayData;
            }      
        }
    }
    Source Code

    为这个类,添加一个方法,arrayToArrayListWithForeach() 即是使用foreach方法,把array数据copy to ArrayList数据集:

     System.Collections.ArrayList _al = new System.Collections.ArrayList();
    
            public void arrayToArrayListWithForeach()
            {
                foreach (int i in ArrayData)
                {
                    _al.Add(i);
                }
            }
    Source Code

    把Array数据copy to ArrayList,还可以使用另外的方法,arrayToArrayListWithAddRange()



     public void arrayToArrayListWithAddRange()
            {
                _al.AddRange(ArrayData);
            }
    Source Code

    为上面的类,写一个ArrayList数据集Sort();

    public void Sort()
            {
                _al.Sort();
            }
    Source Code

    再为类写一个方法,就是输出ArrayList的数据:

     public void Output()
            {
                foreach (int i in _al)
                {
                    Console.WriteLine(i.ToString());
                }
            }
    Source Code

    所需要的方法,均写完,在控制台程序使用它们了。

    上面#17,#18行代码,可以在类new时,一起传入:

    上面#20行代码,由于我们在Bw这个类别中,有写了另外一个方法,所以,也可以这样子:

    OK,实现对数据进行排序:

  • 相关阅读:
    LINUX安装NGINX
    CentOS 设置mysql的远程访问
    centos6 mysql 安装与配置
    php读取用友u8采购入库单列表及详细
    php读取用友u8客户档案
    深度linux没有ll等命令的解决办法
    CentOS7下FTP的安装与配置
    虚拟机CentOS6.5搭建samba服务器实现文件共享
    linux 查找php.ini在那个文件夹
    CBE引擎概览
  • 原文地址:https://www.cnblogs.com/insus/p/10825174.html
Copyright © 2011-2022 走看看